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

# Agent Call Tracing in Tyk AI Studio

> How Tyk AI Studio Agent Call Tracing builds sessions, turns, and spans from gateway traffic, which content it captures, who can read it, and how long it keeps it.

## Availability

| Edition | Deployment Type |
| :- | :- |
| [Enterprise](https://tyk.io/docs/ai-management/ai-studio/overview.md#enterprise-edition) | Self-Managed |

Agent Call Tracing is available from Tyk AI Studio 2.3.0.

Agent Call Tracing shows what an AI agent did, step by step. AI Studio builds the traces from the LLM, tool, MCP, and datasource calls that go through the gateway. The agent needs no SDK or code changes.

The gateway groups the calls into three levels:

| Level | What It Contains |
| :- | :- |
| Session | One conversation or one job of an agent. |
| Turn | One user message or trigger, and all the calls that it causes. |
| Span | One call, such as an LLM call or a tool call. |

You [read each session](https://tyk.io/docs/ai-management/ai-studio/how-to-read-agent-sessions.md) as a transcript or a timeline in the admin console, and export it as JSON or Markdown. AI Studio can also [send finished turns to an OpenTelemetry (OTLP) backend](https://tyk.io/docs/ai-management/ai-studio/how-to-export-agent-traces.md), such as Langfuse or Datadog LLM Observability.

Tracing is off by default. When you [turn it on](https://tyk.io/docs/ai-management/ai-studio/how-to-turn-on-agent-tracing.md), AI Studio captures timing, tokens, cost, models, tool names, and gateway decisions. It captures no prompt or response content until you set a [content mode](#content-capture).

## How Agent Call Tracing Works

AI Studio builds the traces in four stages.

### Capture

The gateway records one event for each call. Each event contains:

* The timing, the App, and the user
* The model or the tool
* The usage and the cost
* The result of each gateway decision, such as a filter, a budget, a fallback, or a routing choice
* The request and response content, if the content mode allows it

The gateway that sees the call processes the content before it stores or sends it. It removes secrets, applies redaction, and limits the size.

### Transport

The embedded gateway of AI Studio writes events to a staging table. An Edge Gateway sends its events to AI Studio in its analytics pulse. AI Studio encrypts the events at rest from the time that it stages them.

### Assembly

One AI Studio replica turns the staged events into sessions, turns, and spans. It waits 30 seconds for late events first. It also links the calls that belong together, and closes idle turns and sessions.

### Read and Export

Administrators read the sessions in **Agent sessions** in the admin console. If you configure exporters, AI Studio also exports finished turns over OTLP. Refer to [Export Agent Traces](https://tyk.io/docs/ai-management/ai-studio/how-to-export-agent-traces.md).

## What the Gateway Traces

The gateway traces only the traffic that goes through it:

* **LLM calls** through `/llm/rest`, `/llm/stream`, and `/ai/`, native API calls, and Bedrock: LLM spans.
  * Each LLM span has the model, tokens, cost, finish reasons, requested tool calls, and gateway decisions.
  * For `/ai/`, there is a span for each provider attempt.
* **Tools, MCP servers, and datasources that AI Studio serves:** Tool and datasource spans. Each span links to the LLM call that asked for it.
* **Edge Gateway plugin endpoints** (`/plugins/`): A span for each call. The gateway does not parse the body.
* **Tools that the agent runs itself**, such as a shell, file edits, or a local MCP server: Inferred spans.
  * The gateway sees the model ask for the tool, and then sees the result in the next request. It records an inferred span between the two.
  * The duration is an upper bound.

Chat in AI Studio, agent plugins, and calls that do not go through the gateway are not traced.

## Permissions

| Permission | Allows |
| :- | :- |
| `traces:read` | Read **Agent sessions**: the session list, transcripts, and timelines, without captured content. Search names and metadata. |
| `trace-content:read` | Read captured content: payloads, previews, content search, and exports with content. Read the real end user ids and external session ids. |
| `traces:execute` | Export sessions. |
| `traces:delete` | Purge sessions and erase the data of an end user. |
| `traces:write` | Change the tracing settings of Apps, LLMs, tools, and datasources. |

The built-in Viewer, Editor, and Auditor roles have `traces:read` and do not have `trace-content:read`. The Editor role also cannot export or purge sessions, or change tracing settings.

`trace-content:read` is sensitive. Grant it only to the users that must read captured content. Refer to [Role-Based Access Control](https://tyk.io/docs/ai-management/ai-studio/rbac.md#built-in-roles).

### Pseudonyms for End Users

The client declares the end user and the external session id of a session. These values can be personal data, such as an email address. A user without `trace-content:read` sees each value as a pseudonym:

* A pseudonym is 32 hex characters. It is always the same for the same value.
* Pseudonyms show in the session list, the session details, the search results, and the exports.
* The user can still filter by the real end user id or session id. The results show pseudonyms. A pseudonym is not a valid filter value.
* The names of AI Studio users are not pseudonyms. AI Studio shows them as they are.

### Content Access and the Audit Trail

AI Studio writes each read of captured content to the [Audit Trail](https://tyk.io/docs/ai-management/ai-studio/audit-trail.md) before it returns the content. This includes payloads, previews, content search, and all exports.

AI Studio also records each purge. The audit record of an end user erasure contains the session ids and a keyed hash of the end user. It does not contain the end user id.

If the audit trail is off (`AUDIT_ENABLED=false`), or cannot accept a record, AI Studio refuses content reads, exports, and purges with `503`. Transcripts then show metadata only.

## Content Capture

The content mode controls what AI Studio stores of prompts, responses, tool arguments, and tool results.

| Mode | What AI Studio Stores |
| :- | :- |
| `off` | No content. AI Studio keeps error messages, redacted and limited to 2 KB. |
| `metadata` | The structure only: the roles and sizes of messages, the key names of tool arguments with the type and size of each value, and the type and size of tool results. |
| `hashed` | Not available in this release. AI Studio stores `metadata`. |
| `redacted` | The content, with secrets removed and the personal data detectors applied. For example, an email address becomes `[REDACTED:EMAIL]`. |
| `full` | The content, with secrets removed. AI Studio does not detect personal data. |

In each mode that keeps content, AI Studio removes secrets:

* It finds field names such as `authorization`, `api_key`, `password`, `cookie`, `client_secret`, `private_key`, and token fields.
* It finds them in nested JSON, in `key=value` pairs, and in base64-encoded values.
* It ignores case and separators.

AI Studio replaces binary content with a reference, unless [AGENT\_TRACING\_PAYLOAD\_STORE\_BINARY](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_payload_store_binary) is `true`.

### How AI Studio Selects the Content Mode

AI Studio selects the content mode of each call in these steps:

1. **The base mode**: The **Content mode** of the App. If the App does not set one, AI Studio uses [AGENT\_TRACING\_CONTENT\_DEFAULT\_MODE](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_content_default_mode). The global ceiling, [AGENT\_TRACING\_CONTENT\_CEILING](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_content_ceiling), limits the base mode.
2. **The request header**: A client can send the `X-Tyk-Trace-Capture` header to ask for a different mode.
   * The header can always lower the mode.
   * The header can raise the mode up to the request ceiling. The request ceiling is [AGENT\_TRACING\_CONTENT\_MAX\_REQUEST\_OVERRIDE](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_content_max_request_override). The **Highest mode a request may ask for** of the App can set a lower request ceiling.
   * The header can never go above the global ceiling.
3. **The resource maximum**: The **Maximum content mode** of the LLM, tool, or datasource limits the result. This also applies to a header that raises the mode.
4. **The store cap**: [AGENT\_TRACING\_STORE\_CONTENT\_MODE\_CAP](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_store_content_mode_cap) is the highest mode that AI Studio ever stores.

* If you do not set `AGENT_TRACING_CONTENT_CEILING`, the global ceiling is the higher of the default mode and the request ceiling. With the default settings, this is `metadata`.
* If the default mode is above the ceiling, AI Studio uses the ceiling and logs a warning. At start, AI Studio logs the Apps whose own mode is above the default mode.
* If AI Studio cannot find the App of a call, or cannot read its settings, it captures no content. A header cannot change this.

To raise the content mode, refer to [Capture Content for All Apps](https://tyk.io/docs/ai-management/ai-studio/how-to-turn-on-agent-tracing.md#capture-content-for-all-apps). For the App and resource settings, refer to [App Settings](https://tyk.io/docs/ai-management/ai-studio/how-to-turn-on-agent-tracing.md#app-settings).

### Examples

With the default settings, the default mode is `off`, the request ceiling is `metadata`, and the store cap is `full`.

| App Content Mode | Other Settings | Request Header | Stored As |
| :- | :- | :- | :- |
| Inherit | Default | None | `off` |
| Inherit | Default | `full` | `metadata`, the request ceiling |
| `redacted` | Default | None | `metadata`, the global ceiling |
| `redacted` | `AGENT_TRACING_CONTENT_CEILING=full` | None | `redacted` |
| `redacted` | `AGENT_TRACING_CONTENT_CEILING=full` | `off` | `off` |
| `full` | `AGENT_TRACING_CONTENT_CEILING=full`, LLM **Maximum content mode** is `metadata` | None | `metadata` |

### Size Limits

* [AGENT\_TRACING\_PAYLOAD\_MAX\_PART\_BYTES](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_payload_max_part_bytes): The maximum size of one content part, such as a request or a tool result.
* [AGENT\_TRACING\_PAYLOAD\_MAX\_SPAN\_BYTES](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_payload_max_span_bytes): The maximum size of all content in one span.
* [AGENT\_TRACING\_UI\_MAX\_PAYLOAD\_VIEW\_BYTES](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_ui_max_payload_view_bytes): The maximum size of content that the admin console shows.

When AI Studio truncates a part, the JSON stays valid. AI Studio marks the cut with `…[truncated N bytes]`.

AI Studio compresses each payload and binds it to its App, span, and part. You cannot move a payload to a different App, span, or part.

## Sessions, Turns, and Correlation

### Sessions

A session is declared or inferred:

* **Declared**: The client sends a session id in a header. All calls with the same id in the same App join the session. Refer to the [Request Headers Reference](https://tyk.io/docs/ai-management/ai-studio/agent-tracing-headers-reference.md).
* **Inferred**: The client does not send a session id. The gateway groups the calls by the App credential, or by the user.
  * Each credential has one rolling session. Agents that use the same App credential at the same time share this session. If agents share an App, declare the sessions.
  * An inferred session closes at its next turn when it reaches [AGENT\_TRACING\_SESSION\_MAX\_DURATION](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_session_max_duration) or [AGENT\_TRACING\_SESSION\_MAX\_SPANS](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_session_max_spans). The next calls start a new inferred session. The session details link the parts with **Continued from** and **Continues in**.

A session closes after the [session idle timeout](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_session_idle_timeout), and a later call opens it again. AI Studio never splits a turn or a declared session.

### Turns

A turn starts when a client calls the model without tool results, for example with a new user message. The turn continues through the calls that send tool results back. It ends when the model answers without a request for tools.

| Turn Status | Meaning |
| :- | :- |
| `ok` | The model answered without a request for more tools. |
| `error` | The last call failed. |
| `abandoned` | The model asked for tools, and no result came back before the [turn idle timeout](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_turn_idle_timeout). |
| `in_progress` | The turn is open. |

If the client sends a W3C `traceparent` header, its trace id identifies the turn. AI Studio never splits this turn.

### Correlation Confidence

The gateway links the calls that belong together. Each link records the confidence of the match:

| Confidence | Meaning |
| :- | :- |
| `declared` | The client declared the link with `traceparent`, or with `_meta.traceparent` on an MCP call. |
| `exact` | The gateway matched an id, such as a `tool_call_id` in the next request, a `previous_response_id`, or a gateway retry or fallback. |
| `strong` | The gateway matched a tool call by its name and arguments, and found one candidate only. |
| `weak` | The gateway matched by name only, or found more than one candidate, or matched across a key rotation. |

A link with a confidence below [AGENT\_TRACING\_MIN\_LINK\_CONFIDENCE](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_min_link_confidence) becomes a candidate link. It does not become a parent. The **Correlation** field of a session shows its weakest link.

Some clients send a tool result without the id of its tool call. For example, Ollama has no tool call ids, and Gemini makes them optional. The gateway still links these results. It uses the id from the conversation history, or it matches the tool name and arguments.

## Retention, Sampling, and Purge

* **Retention**: The retention job runs every 10 minutes.
  * AI Studio deletes sessions, turns, and spans after [AGENT\_TRACING\_METADATA\_RETENTION\_DAYS](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_metadata_retention_days).
  * It deletes captured content after [AGENT\_TRACING\_PAYLOAD\_RETENTION\_DAYS](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_payload_retention_days), or after the **Keep captured content (days)** value of the App. Content never stays longer than metadata.
* **Sampling**: [AGENT\_TRACING\_STORE\_SAMPLE\_RATE](https://tyk.io/docs/ai-management/ai-studio/ai-studio-env.md#agent_tracing_store_sample_rate) sets the share of sessions that AI Studio stores. For other sessions, AI Studio keeps only a summary with counts and cost.
* **Purge a session**: Send `DELETE /api/v1/agent-tracing/sessions/{id}`. AI Studio deletes the spans, links, content, and correlation keys of the session.
  * For a declared session, it also deletes the captures that wait for assembly.
  * For an inferred session, these captures stay, and they can create the session again.
* **Erase an end user**: Send `POST /api/v1/agent-tracing/purge` with the body `{"end_user": "<id>"}`. AI Studio deletes each session that the client declared for this end user, and their waiting captures. Use this for a data subject erasure request.

A purge needs the `traces:delete` permission. The admin console does not have a purge action. A purge does not delete spans that AI Studio already exported. Refer to [Privacy and Erasure](https://tyk.io/docs/ai-management/ai-studio/how-to-export-agent-traces.md#privacy-and-erasure).
