Ready-to-Use Code Patterns

Feenion Recipes & SDK Examples

Explore production code patterns for RAG search pipelines, LangChain callbacks, OpenAI, Gemini, and Claude wrappers.

๐Ÿ” 1. RAG Vector Search & Re-ranking Pipeline

span_type="retrieval"

Trace query embeddings, vector similarity search, document filtering, and final LLM synthesis with relevance scores:

rag_pipeline.py
from feenion import trace, span

@trace(name="rag_knowledge_search", span_type="retrieval")
async def search_and_answer(user_query: str):
    with span("vector_similarity_search", span_type="retrieval"):
        raw_docs = await qdrant_client.search(query=user_query, limit=10)

    with span("cohere_rerank", span_type="retrieval"):
        reranked = rerank_documents(raw_docs, top_n=3)

    with span("synthesize_response", span_type="llm"):
        return await openai_client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": f"Context: {reranked}\n\nQuery: {user_query}"}],
        )

๐Ÿค– 2. Autonomous Multi-Step Agent with Tool Calling

span_type="agent"

Trace tool execution loops, parameter generation, database lookups, and error recovery branches:

agent_tools.py
from feenion import trace, span

@trace(name="financial_analyst_agent", span_type="agent")
def run_financial_analysis(ticker: str):
    with span("fetch_stock_prices", span_type="tool"):
        prices = api.get_stock_history(ticker)

    with span("compute_volatility", span_type="tool"):
        vol = calculate_metrics(prices)

    with span("llm_investment_report", span_type="llm"):
        return generate_memo(ticker, prices, vol)

โœจ 3. Google Gemini Auto-Instrumentation

span_type="llm"

Automatically captures prompt tokens, candidate tokens, finish reasons, latency, and costs using google-genai:

gemini_tracing.py
from google import genai
from feenion.integrations.gemini import instrument_gemini

# 1. Initialize client & instrument in-place:
client = genai.Client()
instrument_gemini(client)

# 2. Call generate_content โ€” trace + token metrics auto-captured!
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Explain vector similarity search algorithms."
)

๐Ÿงช 4. Zero-Key Mock AI Ecosystem (MCP, RAG & Multi-LLM)

Run Locally

Run the complete standalone example script that mocks Google Gemini, OpenAI, Vector DB RAG retrieval, and Model Context Protocol (MCP) tools:

examples/comprehensive_mock_ecosystem.py Zero API Keys Needed
# Run directly from terminal:
$ python examples/comprehensive_mock_ecosystem.py

# Output: Full multi-span trace hierarchy with flamegraph latency,
# model token counts, tool execution parameters, and calculated costs!