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

# How do I track and cap LLM token spend per AI agent?

> Track prompt and completion tokens and cost for each AI agent in Tyk AI Studio, set a hard monthly budget for each agent, and stop an agent that loops.

Register each AI agent as its own [App](/docs/ai-management/ai-studio/ai-portal-app) in [Tyk AI Studio](/docs/ai-management/ai-studio/overview). AI Studio [records](/docs/ai-management/ai-studio/analytics) the prompt tokens, completion tokens, and cost of every LLM call against the App. Set a [monthly budget](/docs/ai-management/ai-studio/budgeting) on the App. When the agent reaches the budget, AI Studio [refuses its LLM requests](/docs/ai-management/ai-studio/budgeting#enforcement) with HTTP 403. AI Studio also [alerts](/docs/ai-management/ai-studio/notifications) the App owner at 80% and 100%.

| Item | Detail |
| - | - |
| Applies to | LLM traffic through Tyk AI Studio and its Edge Gateways |
| Edition | Budget enforcement, Team budgets, and analytics dashboards: Enterprise Edition. In Community Edition, AI Studio records spend but does not block requests. |
| Version | Tyk AI Studio 2.2 or later. Agent and user fields in proxy logs: 2.2.1 or later. |

***

## Why Track Spend for Each Agent

An agent calls the LLM in a loop until it finishes its task. A bad prompt, a tool error, or a retry loop can make it run for hours. If all agents share one key, the invoice shows one total, and you cannot find the agent that caused it. For FinOps, each cost record needs the agent, the model, and the team.

AI Studio calculates cost from the token counts in each LLM response and the [model prices](/docs/ai-management/ai-studio/model-prices) that you set. It does not estimate cost from your invoice.

***

## How Do I Attribute LLM Token Costs to an Agent, Team, or App?

Create one App for each agent. The App is the identity that AI Studio uses for cost, budgets, and access.

1. Create an App for the agent, and give it a name that identifies the agent, for example `invoice-reconciler`. See [Creating an App](/docs/ai-management/ai-studio/ai-portal-app#creating-an-app).
2. Grant the App access only to the LLMs and tools that the agent needs.
3. Select the App's Team. The Team is the cost center for [Team budgets](/docs/ai-management/ai-studio/budgeting#team-budgets).
4. Activate the App's credentials. See [App lifecycle](/docs/ai-management/ai-studio/admin-apps#app-lifecycle).
5. Give the App's credentials to that agent only.

For every LLM call, AI Studio then records:

| Field | Description |
| - | - |
| App ID | The agent |
| LLM ID and model | The LLM configuration and the model, for example `gpt-4o` |
| Prompt tokens and completion tokens | Input and output tokens. Cache read and cache write tokens are separate. |
| Cost | Calculated from the token counts and the model price |
| Timestamp and latency | When the call happened and how long it took |
| Response code | The HTTP status of the call |

AI Studio adds the costs of each App to its Team. The **Team Costs** table on the dashboard shows the cost, share, tokens, and requests of each Team.

If agents get tokens from your identity provider instead of App keys, use the [OAuth2 Client Credentials plugin](/docs/ai-management/ai-studio/oauth2-client-credentials) (Enterprise, 2.2.1 or later). It maps each OAuth client to an App. It can also create an App for each new client from a template.

***

## How Do I Set a Hard Budget Cap on LLM Spend per Agent or Team?

Set a monthly budget on the agent's App. In the App form, set the budget to **Fixed amount** and enter the limit. You can also set budgets on an LLM configuration and on a Team. AI Studio checks every budget that applies to a request, and refuses the request if one of them is spent.

When the App's budget is spent, the gateway returns this error on the OpenAI-compatible endpoints:

| HTTP status | `code` | Meaning |
| - | - | - |
| `403` | `budget_exceeded` | A budget that applies to the request is spent. |
| `503` | `budget_check_unavailable` | The gateway cannot read the budget. Retry later. |

The budget period is one month. For all budget settings, see [Budget control](/docs/ai-management/ai-studio/budgeting).

AI Studio does not have a per-App limit for requests per minute (RPM) or tokens per minute (TPM). The monthly budget is the cap on spend.

***

## The Agent Found It Can Call an Expensive API in a Loop: Where Does the Kill Switch Go?

Put it where the loop spends money.

| The loop calls | Kill switch | Effect |
| - | - | - |
| An LLM through AI Studio | The App's monthly budget | AI Studio refuses LLM requests when the budget is spent. |
| An LLM through AI Studio, now | Set the App budget to `$0`, or set the App's credentials to **Inactive** | AI Studio refuses all requests from the App. |
| An MCP tool or a REST API through Tyk Gateway | A rate limit and a quota in the agent's policy | Tyk Gateway returns `429` when the agent reaches a limit. |
| An MCP tool or a REST API through Tyk Gateway, now | Revoke the agent's key in the Tyk Dashboard | Tyk Gateway refuses the next request with that key. |

A budget stops a loop only after the loop has spent the budget. A rate limit stops it within seconds. Use both for agents that can call paid APIs. For per-tool rate limits, see [MCP Gateway policies](/docs/ai-management/mcp-gateway/policies). For quotas, see [Request quotas](/docs/api-management/request-quotas).

***

## How Do I Track LLM Costs and Latency in Production?

AI Studio gives you three views of the same data:

| View | Use it to |
| - | - |
| Analytics dashboard in AI Studio | Filter cost, tokens, requests, and latency by App, user, LLM, and time range |
| Analytics API, for example `GET /api/v1/analytics/team-costs` | Export cost data to your own reports or FinOps tool |
| Prometheus `/metrics` endpoint | Alert on `aistudio_llm_cost_total`, `aistudio_llm_tokens_total`, and `aistudio_llm_request_duration_seconds` |

For the dashboard and the metrics, see [Analytics](/docs/ai-management/ai-studio/analytics). For OpenTelemetry traces, see [Observability](/docs/ai-management/ai-studio/observability).

You must turn on analytics in the configuration of each Edge Gateway. If the dashboard shows no data for a gateway, see [Edge Gateway analytics configuration](/docs/ai-management/ai-studio/analytics#edge-gateway-analytics-configuration).

***

## I Need a Log of Every Call an Agent Made, and Which User It Was Acting As

AI Studio stores a proxy log row for every LLM call. The App and LLM pages show these rows.

The `user_id` of a row is the App owner, not the end user. To record the end user, use one of these methods:

* Authenticate the agent with the [OAuth2 Client Credentials plugin](/docs/ai-management/ai-studio/oauth2-client-credentials#who-made-the-call) (2.2.1 or later). For a delegated token, the proxy log stores the user in `on_behalf_of` and the agent in `acting_agent`. The App pages show these values as **For** and **Agent**.
* Create a separate App for each end user or tenant. Each App then has its own budget and its own log.

The `on_behalf_of` and `acting_agent` fields are for audit only. AI Studio does not use them for budgets or authorization. Billing stays with the App.

Tool calls through Tyk Gateway go to the [MCP access logs](/docs/ai-management/mcp-gateway/mcp-access-logs). Each record shows the JSON-RPC method, the tool name, and a hash of the consumer's key.

***

## Verify the Setup

1. Set a small budget on a test App, for example `$1`.
2. Send LLM requests with the App's credentials until the App's cost passes `$1`.
3. Make sure that the next request fails with `403` and the code `budget_exceeded`.
4. Make sure that the App owner received the 80% and 100% alerts. See [Notifications](/docs/ai-management/ai-studio/notifications).
5. Open the App page. The proxy log shows the tokens and the cost of each call.
