> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fiddler.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# FiddlerAgentMiddleware

> LangChain V1 middleware that instruments `create_agent` calls with Fiddler tracing.

LangChain V1 middleware that instruments `create_agent` calls with Fiddler tracing.

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:

```python theme={null}
from langchain.agents import create_agent
from fiddler_otel import FiddlerClient
from fiddler_langchain import FiddlerAgentMiddleware

client = FiddlerClient(api_key="...", application_id="...", url="...")

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[...],
    middleware=[FiddlerAgentMiddleware(client=client, agent_name="my_agent")],
)
agent.invoke({"messages": [...]})
```

## Parameters

<ParamField path="client" type="FiddlerClient" required={true}>
  The `FiddlerClient` instance.
</ParamField>

<ParamField path="agent_name" type="str" required={false} default="&#x22;agent&#x22;">
  Label applied to the root Agent span. Defaults to `"agent"`.
</ParamField>

## 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`

## wrap\_tool\_call()

Wraps a synchronous tool call with a Tool span.

### Returns

`Any`

## *async* awrap\_tool\_call()

Wraps an asynchronous tool call with a Tool span.

### Returns

`Any`
