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

# Multi-Agent Traces

> See which agent ran each span, follow handoffs between agents, and group the traces of one session.

A multi-agent run is one trace, or a few related traces, whose spans come from several agents. SGP
Traces records which agent ran each span, where one agent handed work to another, and which
session a run belongs to. You read them the same way in the UI and the API, whichever framework
sent the spans.

## What you get

| Concept | What it is | Where you see it |
| - | - | - |
| **Agent** | The agent a span ran as | A badge on each span in the [Trace Detail View](https://docs.gp.scale.com/docs/v5/tracing/tracing-ui/trace-detail-view.md), the Agent Name and Agent ID filters on the [Traces page](https://docs.gp.scale.com/docs/v5/tracing/tracing-ui/traces-table.md), `agent_name` and `agent_id` on each span summary of a trace, and `__agent_name__` and `__agent_id__` in the span's `metadata` |
| **Session** | The conversation or thread a run belongs to | The [Session ID filter](https://docs.gp.scale.com/docs/v5/tracing/tracing-ui/traces-table.md#session-id) on the Traces page, the Session row in the span details panel, and `group_id` on every span |
| **Handoff** | One agent passing work to another | `__handoff_to__` in the `metadata` of the span that hands off, holding the target agent's name |
| **Parent run** | The run that started this one, when the started run has a trace of its own | `__parent_trace_id__` in the `metadata` of the started run's root span |

Who called whom inside one trace is each span's `parent_id`, as in any trace.

Two rules apply to all of them:

* **A span's agent is its own.** A span that names no agent belongs to the nearest span above it
  that does. Nothing is copied down the tree, so the agent filters return only the spans that name
  the agent.
* **What you send wins.** A key you set yourself, such as `__agent_name__`, is stored as sent and
  never overwritten.

## What to send

### Over OpenTelemetry

When spans arrive at the [OpenTelemetry endpoint](https://docs.gp.scale.com/docs/v5/tracing/integrations/opentelemetry.md), SGP
derives each field from the attributes agent frameworks already send. To emit them yourself, set:

| Field | Attribute | On which spans |
| - | - | - |
| Agent | `gen_ai.agent.name` and a stable `gen_ai.agent.id` | Every span the agent produces, at minimum its `invoke_agent` span |
| Session | `gen_ai.conversation.id` | Every span of the run, with the same value across the turns and agents of one conversation |
| Handoff | `gen_ai.transfer.target.name` on the tool call that transfers, or `gen_ai.handoff.to_agent` on an `agent_handoff` span | The span that hands off |
| Parent run | `__parent_trace_id__`, holding the calling run's trace ID as SGP stores it, which is 32 lowercase hex characters for an OpenTelemetry trace | The root span of a run that starts a trace of its own. You do not need it when you propagate W3C `traceparent`, because the called agent joins the caller's trace |

```python theme={null}
from opentelemetry import trace

tracer = trace.get_tracer("support-desk")
session = "support-chat-42"

with tracer.start_as_current_span(
    "invoke_agent triage",
    attributes={
        "gen_ai.operation.name": "invoke_agent",
        "gen_ai.agent.name": "triage",
        "gen_ai.agent.id": "agent-triage-1",
        "gen_ai.conversation.id": session,
    },
):
    with tracer.start_as_current_span(
        "agent_handoff",
        attributes={
            "gen_ai.operation.name": "agent_handoff",
            "gen_ai.agent.name": "triage",
            "gen_ai.handoff.to_agent": "billing",
            "gen_ai.conversation.id": session,
        },
    ):
        ...
```

Many frameworks leave the session out. A span processor adds it to every span they produce:

```python theme={null}
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider

SESSION = "support-chat-42"


class SessionID(SpanProcessor):
    def on_start(self, span, parent_context=None) -> None:
        span.set_attribute("gen_ai.conversation.id", SESSION)


provider = TracerProvider()
provider.add_span_processor(SessionID())
```

Register the exporter on the same provider as in
[Configure the exporter](https://docs.gp.scale.com/docs/v5/tracing/integrations/opentelemetry.md#configure-the-exporter). SGP
also reads the OpenInference and LangChain spellings of these attributes, listed under
[How spans map to SGP](https://docs.gp.scale.com/docs/v5/tracing/integrations/opentelemetry.md#how-spans-map-to-sgp).

### Through the SGP SDK or the Agentex SDK

Spans written through the [SGP SDK](https://docs.gp.scale.com/docs/v5/tracing/sdk/creating-traces-spans.md) or the Agentex SDK
are stored as sent. Nothing is derived, so set the fields yourself:

| Field | Set |
| - | - |
| Session | `group_id` on the trace. Spans created inside it inherit it |
| Agent | `__agent_name__` and `__agent_id__` in the span's `metadata` |
| Handoff | `__handoff_to__` in the `metadata` of the span that hands off |
| Parent run | `__parent_trace_id__` in the `metadata` of the started run's root span |

```python theme={null}
import scale_gp_beta.lib.tracing as tracing

with tracing.create_trace(name="support-desk", group_id="support-chat-42"):
    with tracing.create_span("triage", metadata={"__agent_name__": "triage"}):
        with tracing.create_span(
            "transfer_to_billing",
            metadata={"__agent_name__": "triage", "__handoff_to__": "billing"},
        ):
            ...
    with tracing.create_span("billing", metadata={"__agent_name__": "billing"}):
        ...
```

The Agentex SDK sets `__agent_name__` on every span from the agent's manifest, and `__agent_id__`
when the `AGENT_ID` environment variable is set. It sends no session, handoff, or parent run.

## What each framework sends

Each OpenTelemetry row reflects a recorded run of that framework. A framework that is not listed
works the same way when it sends the attributes above.

| Framework | Agent | Session | Handoff | What you add |
| - | - | - | - | - |
| Google ADK, built-in OpenTelemetry tracing | Name on every span inside an agent | On workflow, agent, and model spans, never on tool spans. A sub-agent called through `AgentTool` runs in a session of its own | `AgentTool` calls. `transfer_to_agent` calls only with content capture on | `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_ONLY`, so a transfer names its target |
| OpenAI Agents SDK, `opentelemetry-instrumentation-openai-agents-v2` | Name on `invoke_agent` spans only. Model and tool call spans name no agent | None. `RunConfig.group_id` never reaches a span | `agent_handoff` spans. An agent called as a tool is not a handoff. It shows as its `invoke_agent` span under the tool span | The session, with a span processor |
| Pydantic AI, built-in instrumentation | Name on every span | On every span. A delegate agent runs in a session of its own unless the caller passes it the conversation ID | None. A delegation is an agent run inside a tool. It shows as the delegate's `invoke_agent` span under the tool span | The caller's conversation ID on the delegate's run, to keep one session |
| Claude Agent SDK, `otel-instrumentation-claude-agent-sdk` | Name on the `invoke_agent` span only, and only when you pass `agent_name` to the instrumentor. Subagents and their tool calls name no agent | On the `invoke_agent` span only | `Agent` tool calls, from their `subagent_type`, only with content capture on | `agent_name` and `capture_content=True` on the instrumentor, and the session on the other spans with a span processor |
| LangGraph and LangChain, LangSmith OpenTelemetry exporter | Name on every span inside an agent built with `create_agent`. Agents built with `create_react_agent`, which includes every `langgraph-supervisor` supervisor, have no name | The run's `thread_id` metadata | Supervisor and swarm `transfer_to_*` tool spans. A return to the supervisor makes no span | Build agents with `create_agent` to name them. Agents built with `create_react_agent` stay unnamed |
| Agentex SDK | Name on every span, and ID when `AGENT_ID` is set | None | None | [Trace ID propagation](https://docs.gp.scale.com/docs/v5/tracing/integrations/agentex/multi-agent-tracing.md). Without it an agent-to-agent call is two unrelated traces |

## Query it

### In the UI

* On the [Traces page](https://docs.gp.scale.com/docs/v5/tracing/tracing-ui/traces-table.md), the **Session ID** filter lists
  one row per trace of a session. The **Agent Name** and **Agent ID** filters match the spans that
  name an agent, so use the **All Spans** scope to find an agent that did not start the trace.
* In the [Trace Detail View](https://docs.gp.scale.com/docs/v5/tracing/tracing-ui/trace-detail-view.md), each span that names
  an agent shows an agent badge, and the span details panel shows the span's session with a
  shortcut that filters the Traces page to it.

### Through the API

Send these request bodies to [Search Spans](https://docs.gp.scale.com/reference/v5/spans/search-spans.md)
(`POST /v5/spans/search`):

| Question | Request body |
| - | - |
| Which spans did an agent produce? | `{"agentex_agent_names": ["billing"]}`, or `agentex_agent_ids` for agent IDs |
| Which traces belong to a session? | `{"group_id": "support-chat-42", "parents_only": true}` |
| Which spans carry a session themselves? | `{"group_id": "support-chat-42"}` |
| Where was work handed to an agent? | `{"extra_metadata": {"__handoff_to__": "billing"}}` |
| Which runs did a trace start? | `{"parents_only": true, "extra_metadata": {"__parent_trace_id__": "<calling trace ID>"}}` |

With `parents_only`, a session search returns the root span of each trace that holds a completed
span of the session inside the time window, whether or not the root carries the session itself.
The time window and the other filters still apply to that root.

```bash theme={null}
curl -X POST "$SGP_CLIENT_BASE_URL/v5/spans/search?from_ts=<start>&to_ts=<end>" \
  -H "x-api-key: $SGP_API_KEY" \
  -H "x-selected-account-id: $SGP_ACCOUNT_ID" \
  -H "Content-Type: application/json" \
  -d '{"group_id": "support-chat-42", "parents_only": true}'
```

`<start>` and `<end>` are ISO 8601 timestamps.
[Span metrics](https://docs.gp.scale.com/reference/v5/spans/get-time-bucketed-span-metrics.md) and
[Export Spans](https://docs.gp.scale.com/reference/v5/spans/export-spans.md) take the same filters and resolve the same traces.
An export with `with_children` set to `true` carries every span of those traces.

To read a whole run, call
[List span summaries for a trace](https://docs.gp.scale.com/reference/v5/spans/list-span-summaries-for-a-trace.md)
(`GET /v5/traces/{trace_id}/spans`). Each summary carries `parent_id`, `group_id`, and the
`agent_name` and `agent_id` its span ran as. A summary whose span names no agent has both set to
`null`, so walk up `parent_id` to the nearest span that names one.

## Limits

| Limit | What you see |
| - | - |
| Fields are derived at ingest | A span ingested by a release that did not derive these fields stays without them. Nothing backfills it |
| No inheritance | The agent filters, the agent badge, and `agent_name` cover only the spans that name an agent |
| A handoff needs a named target | A transfer named only in a tool's name, such as `transfer_to_billing`, or named only in tool arguments while content capture is off, has no `__handoff_to__` |
| SDK spans | Nothing is derived for spans written through the SGP SDK or the Agentex SDK |
| Unusable values | A session ID, agent name, agent ID, or handoff target that is blank, longer than 256 bytes, or masked by a redaction processor as `****` is skipped. The next attribute in order is read instead |
| Spans in progress | A session search with `parents_only` finds a trace once one of the session's spans in it has completed |
| Very large sessions | Some deployments cap how many traces of one session a request can resolve inside its time window. The [`group_id` filter reference](https://docs.gp.scale.com/reference/v5/spans/search-spans#body-group-id-one-of-0) says which. Past the cap, a search still returns the same root spans but runs slower. With `allow_partial_results=true` on a `start_timestamp` sort, a page can end early with `search_status` set to `partial` and a `next_cursor` that resumes it. Any other search that cannot fill a page is refused with `400` and a `query too broad` detail, and so is every metrics request for such a session. An export has a higher cap of 2,000,000 traces and fails with the reason `QUERY_TOO_BROAD` past it. Narrow the time window to stay under the cap |
| Legacy trace store | On accounts still served by the legacy trace store, a session search with `parents_only` returns only the root spans that carry the session themselves, and every span summary has `agent_name` and `agent_id` set to `null` |
| Classic Traces page | The classic Traces page (`/classic/traces`) has no Session ID filter and does not export by session |
