This middleware produces a clean, flat trace hierarchy for agents built with
langchain.agents.create_agent:
- One root Agent span (opened in
before_agent, closed in after_agent)
- One LLM child span per model invocation (
wrap_model_call / awrap_model_call)
- One Tool child span per tool invocation (
wrap_tool_call / awrap_tool_call)
All spans are written to Fiddler’s isolated OpenTelemetry context so they do not
interfere with any global tracer that may be active in the application.
Concurrent invocations (async) are fully isolated: Python’s ContextVar ensures
each agent.ainvoke() task tracks its own root span independently.
Usage:
Parameters
The FiddlerClient instance.
Label applied to the root Agent span. Defaults to "agent".
before_agent()
Opens a root Agent span at the start of each agent invocation.
If the current context already has a Fiddler span (e.g. we are being invoked
as a sub-agent from a tool call), the new agent span is created as a child
of that span so the full multi-agent flow appears in a single trace.
Returns
dict[str, Any] | None
after_agent()
Closes the root Agent span at the end of each agent invocation.
Returns
dict[str, Any] | None
wrap_model_call()
Wraps a synchronous model call with an LLM span.
Returns
ModelResponse
async awrap_model_call()
Wraps an asynchronous model call with an LLM span.
Returns
ModelResponse
Wraps a synchronous tool call with a Tool span.
Returns
Any
Wraps an asynchronous tool call with a Tool span.
Returns
Any