# turn

Canonical URL: https://developers.iagenify.com/sdk/agent/events/turn/
Language: en

`{ type: "turn", model, durationMs, inputTokens, cachedTokens, contextTokens, contextLimit, contextParts, contextCleared, credits, totalCredits }`

One per step, after the model answers. It carries what the step cost, how full the context is, and which model actually served it.

Status: one per step · measured, not estimated · totalCredits is the run

## Fields {#fields}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `model` | string |  | Which model served **this** turn. Not always the one you asked for — a [fallback](/sdk/agent/fallback-and-retry) changes it per turn. |
| `durationMs` | integer |  | How long the turn took. |
| `inputTokens` | integer|null |  | What the provider counted on the way in. |
| `cachedTokens` | integer |  | How much of that the provider served from its cache. |
| `contextTokens` | integer |  | The size of the conversation as measured. The same number as `inputTokens` when it is available. |
| `contextLimit` | integer |  | The limit in force — your `contextTokens`, or the default. |
| `contextParts` | object |  | `{ system, tools, memory, messages }`, scaled so they add up to the total. |
| `contextCleared` | integer |  | How many tool results have been emptied so far in this run. |
| `credits` | integer |  | What this turn cost. |
| `totalCredits` | integer |  | What the run has cost so far — **turns and paid tools together**. |

## A context gauge {#gauge}

```javascript
onEvent: (e) => {
  if (e.type !== "turn") return;
  const pct = Math.round((e.contextTokens / e.contextLimit) * 100);
  console.log(`${pct}% of the window · ${e.cachedTokens} cached · ${e.contextCleared} results cleared`);
  console.log(e.contextParts);   // { system: 2100, tools: 3900, memory: 180, messages: 25024 }
};
```

> **The total is measured; the four parts are estimated and scaled**
>
> The provider gives the total. The parts are estimated from their size and then scaled so the four add up to it exactly — the proportions are sound, an individual part is close rather than exact.

## totalCredits already counts the tools {#credits}

> **Do not add a tool's meta.credits on top of it**
>
> A paid tool's cost is added to the run total when the tool finishes, so the next turn event already includes it. Adding both counts it twice. And a balance read by the account tool is a balance, not a cost — treating it as one makes a run look enormously expensive.

## Which model is in there {#model}

The field is the model that served the turn. Watching it is how a silent degradation becomes visible: a run that starts on one model and finishes on a fallback has had a bad time, even if every answer was fine.

## cachedTokens is worth showing {#cache}

A long conversation whose prefix is stable is served largely from cache, and cached input is billed at a fraction of the normal rate. A number that drops to zero between two turns means something near the start of the prompt changed — a new tool, a new storage, a cleared batch.

> **A subagent's events arrive here too**
>
> They carry parentId — the identifier of the task call that started them — and agent, its name. Their own id is prefixed with the task's, so two runs never collide. Test parentId before treating an event as the main run's.

- [Context](/sdk/agent/context): What the parts mean, and what gives way first.
- [Budget](/sdk/agent/budget): What totalCredits is checked against.

```python
# Python has no "turn" event: read agent.context and agent.usage whenever a tool finishes
def on_event(event):
    if event["type"] != "tool_end":
        return
    context = agent.context
    pct = round(context["tokens"] / context["limit"] * 100)
    print(f"{pct}% of the window · {context['cleared']} results cleared")
    print(context["parts"])   # {"system": 2100, "tools": 3900, "memory": 0, "messages": 25024}
```
