Skip to main content

Fiddler LangChain SDK

PyPI Instrument your LangChain V1 agents built with langchain.agents.create_agent for comprehensive agentic observability. The Fiddler LangChain SDK produces a clean, flat trace hierarchy — agent → LLM calls → tool calls — with no noisy Chain wrappers. One call to FiddlerLangChainInstrumentor.instrument() auto-traces every agent in your application.
Using LangChain prior to v1? The fiddler-langchain SDK requires LangChain v1 (langchain.agents.create_agent API). For applications using earlier LangChain versions or LangGraph workflows, use the Fiddler LangGraph SDK instead — it covers both LangGraph and earlier LangChain-based agents.

What you’ll need

  • Fiddler account (cloud or on-premises)
  • Python 3.10-3.14
  • LangChain V1 application using langchain.agents.create_agent
  • Fiddler API key and application ID

Quick start

Get monitoring in 4 steps:
That’s it! Your agent traces are now flowing to Fiddler.
Important: Use langchain.agents.create_agent (the module attribute), not from langchain.agents import create_agent. Call instrument() before importing create_agent if you use the from ... import style so the local name is bound to the patched version.

What gets monitored

Trace hierarchy

Each agent invocation produces a clean, flat trace with no noisy Chain wrappers:

Captured data

Agent root span:
  • Agent name and agent ID
  • Conversation ID (if set via set_conversation_id())
LLM spans (per model invocation):
  • Model name and provider
  • System prompt and user prompt (last human message)
  • Full input message history (gen_ai.input.messages)
  • LLM completion and output messages (gen_ai.output.messages)
  • Token usage (input, output, total)
  • LLM context (if set via set_llm_context())
  • Available tool definitions
Tool spans (per tool call):
  • Tool name, input arguments, and output

Application setup

Before instrumenting your application, you must create an application in Fiddler and obtain your Application ID.

1. Create your application in Fiddler

Log in to your Fiddler instance and navigate to GenAI Applications, then click Add Application and follow the onboarding wizard to create your application.

2. Copy your Application ID

After creating your application, copy the Application ID from the GenAI Applications page. This must be a valid UUID4 format (for example, 550e8400-e29b-41d4-a716-446655440000). You’ll need this for initialization.

3. Get your API key

Go to Settings > Credentials and copy your API key. You’ll need this for initialization.

Detailed setup

Installation

Framework Compatibility:
  • LangChain V1: >= 1.0.0 — agents built with langchain.agents.create_agent
  • Python: 3.10-3.14
  • fiddler-otel: >= 1.0.0 (installed automatically)
  • OpenTelemetry: API and SDK >= 1.27.0 (installed automatically)

Configuration

Using environment variables

Environment Variables Reference:

Instrumentation methods

The Fiddler LangChain SDK provides two instrumentation approaches:

Auto-instrumentation

FiddlerLangChainInstrumentor.instrument() monkey-patches langchain.agents.create_agent once. Every subsequent call to create_agent() automatically receives a FiddlerAgentMiddleware. No changes to individual agent creation calls are needed.
Key behaviors:
  • Idempotent: Calling instrument() multiple times is safe — it will not create duplicate middleware.
  • Agent naming: If name='...' is passed to create_agent(), that name is used for the agent in traces. If omitted, no agent name is set and the agent appears without a label in the UI.
  • Existing middleware preserved: If you pass a FiddlerAgentMiddleware instance manually in middleware=[...], the instrumentor skips injection for that call so your manual configuration is preserved.
Uninstrumenting:

Manual middleware

For per-agent control, pass FiddlerAgentMiddleware directly to create_agent() without using the instrumentor:
Use manual middleware when you want to trace only specific agents, or when you need different configurations per agent.

Advanced usage

Multi-turn conversations

Use set_conversation_id() to link multiple agent invocations into a single conversation in the Fiddler UI. All agents in the application that share the same conversation_id appear together in conversation-level views.

LLM context

Attach contextual metadata to LLM spans by calling set_llm_context() before the agent runs. The instrumentation reads this value from the model’s metadata at invocation time and records it as gen_ai.llm.context on every LLM span for that model.
set_llm_context() accepts both plain model instances (BaseLanguageModel) and RunnableBinding instances (for example, models wrapped with .with_config() or .bind_tools()).

Clearing LLM context for non-RAG steps

In multi-step agent workflows, context set after a RAG retrieval step leaks into subsequent non-RAG LLM calls (tool planning, routing, etc.), causing unintended faithfulness evaluation. Use clear_llm_context() to explicitly remove context before non-RAG steps:
clear_llm_context(model) is equivalent to set_llm_context(model, None).

Span-level attributes

Use add_span_attributes() to attach custom metadata to a specific LangChain component (model, tool, or retriever). The middleware reads these attributes when creating the span for that component and records them as fiddler.span.user.{key}.
Unlike add_session_attributes (which applies to every span in the context), add_span_attributes is scoped to a single component.

Session attributes

Use add_session_attributes() to attach metadata that appears on every span created in the current thread or async coroutine. Use this for user-level or environment-level metadata that applies to the whole session.

Retriever instrumentation

The LangChain V1 middleware does not expose a dedicated retriever hook. Following the same convention used in fiddler-langgraph, retrievers are treated as tools. Wrap your retriever with @tool (or use create_retriever_tool) and pass it to create_agent. The middleware’s tool hook captures the retriever call automatically as a TYPE=tool span — with the query as tool_input and the retrieved documents as tool_output.
The resulting trace:

Multi-agent setup

With the instrumentor, a single instrument() call patches create_agent so every agent is traced. Pass name='...' to each create_agent() to label agents in traces. When a sub-agent is invoked from within a delegation tool, its root Agent span is automatically created as a child of the tool span — the entire multi-agent flow appears in a single trace. No manual linking is needed: wrap_tool_call attaches the active tool span into the OTel context before invoking the handler, and before_agent detects that active span and nests under it.
Trace output (all agents appear in a single trace):
set_conversation_id() is useful for linking multiple top-level invocations (e.g., multi-turn conversations) — not for joining sub-agents within a single invocation, since they already share the same trace automatically.

Async agents

The instrumentation fully supports async agents via the awrap_model_call and awrap_tool_call hooks. Use agent.ainvoke() — no additional configuration needed:
The instrumentation automatically uses the async lifecycle hooks when the agent is invoked asynchronously, producing the same span hierarchy as the sync path.

Error handling

If an LLM call or tool call raises an exception, the instrumentation:
  1. Catches the exception and marks the failing span with StatusCode.ERROR
  2. Re-raises the exception so normal error handling in your application is unaffected
  3. Cleanly closes the root agent span — no dangling open spans
This means partial traces are never lost — all spans up to the point of failure are recorded and visible in Fiddler.

Running in AWS SageMaker

Because fiddler-langchain exports traces through FiddlerClient, it inherits the Fiddler OTel SDK’s AWS SageMaker Partner App authentication. Install the sagemaker extra (pip install "fiddler-otel[sagemaker]") and set the AWS_PARTNER_APP_AUTH, AWS_PARTNER_APP_ARN, and AWS_PARTNER_APP_URL environment variables — your instrumentation code is unchanged.

Flush and shutdown

Local debugging

JSONL file capture (save a local copy of spans in addition to Fiddler export): jsonl_capture_enabled=True is additive — spans are saved to a local JSONL file and continue to be exported to Fiddler via OTLP. Setting this to True does not suppress or disable the OTLP export to Fiddler.
Override the output file path via environment variable:
Each line in the output file is a JSON object. Fields:

Relationship to fiddler-langgraph

Both packages depend on fiddler-otel for the core FiddlerClient and span wrappers.

What’s next?