Skip to content

LangChain Agent

This walkthrough covers three LangChain examples of increasing complexity, all automatically instrumented with Coalex.

Source: examples/agent-python/langchain_example.py


What This Example Demonstrates

  • auto_instrument() capturing all LangChain spans with zero manual code
  • Three LangChain patterns: simple invocation, LCEL chain, multi-step reasoning
  • coalex_context() scoping each example as a separate trace
  • Multi-provider support: Vertex AI (Gemini), OpenAI, Anthropic

Setup

docker compose up -d
export COALEX_API_KEY="ck_live_..."
export COALEX_ENDPOINT="http://localhost:8080"

cd examples/agent-python
uv pip install -e ".[dev]"
uv run python langchain_example.py

Example 1: Simple LLM Invocation

A direct LLM call wrapped in a Coalex context:

with coalex.coalex_context(agent_id="demo-simple", request_id=str(uuid.uuid4())):
    response = llm.invoke("Explain what LangChain is in one sentence.")
    print(response.content)

Captured spans:

Trace: demo-simple / req-<uuid>
  └── ChatGoogleGenerativeAI (LLM)
       model: gemini-2.0-flash
       tokens_in: 12, tokens_out: ~35

Example 2: LCEL Chain

An LCEL chain composing a prompt template, LLM, and output parser:

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a poet who writes in the style of {style}."),
    ("human", "Write a short poem about {topic}."),
])
chain = prompt | llm | StrOutputParser()

with coalex.coalex_context(agent_id="demo-chain", request_id=str(uuid.uuid4())):
    result = chain.invoke({"style": "Shakespeare", "topic": "artificial intelligence"})

Captured spans:

Trace: demo-chain / req-<uuid>
  └── RunnableSequence (CHAIN)
       ├── ChatPromptTemplate (CHAIN)
       ├── ChatGoogleGenerativeAI (LLM)
       │     model: gemini-2.0-flash
       └── StrOutputParser (CHAIN)

Auto-instrumentation captures each step of the LCEL pipeline as a child span.


Example 3: Multi-Step Reasoning

A three-step insurance claims processing workflow:

with coalex.coalex_context(agent_id="demo-reasoning", request_id=str(uuid.uuid4())):
    # Step 1: Extract key facts
    facts = llm.invoke(f"Extract key facts from: {claim_text}")

    # Step 2: Assess risk
    assessment = llm.invoke(f"Assess risk for: {facts.content}")

    # Step 3: Generate recommendation (LCEL chain)
    rec_chain = rec_prompt | llm | StrOutputParser()
    recommendation = rec_chain.invoke({
        "facts": facts.content,
        "assessment": assessment.content,
    })

Captured spans:

Trace: demo-reasoning / req-<uuid>
  ├── ChatGoogleGenerativeAI (LLM) -- Step 1
  ├── ChatGoogleGenerativeAI (LLM) -- Step 2
  └── RunnableSequence (CHAIN) -- Step 3
       ├── ChatPromptTemplate
       ├── ChatGoogleGenerativeAI (LLM)
       └── StrOutputParser

All three steps are captured under the same trace.


Provider Selection

COALEX_LLM_PROVIDER Model Required
vertex (default) gemini-2.0-flash gcloud auth application-default login
openai gpt-4o OPENAI_API_KEY
anthropic claude-3-5-sonnet-20241022 ANTHROPIC_API_KEY

Override with COALEX_MODEL:

COALEX_LLM_PROVIDER=openai COALEX_MODEL=gpt-4o-mini uv run python langchain_example.py

Next Steps