Events

turn

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

one per stepmeasured, not estimatedtotalCredits is the run
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Fields

modelstring

Which model served this turn. Not always the one you asked for — a fallback changes it per turn.

durationMsinteger

How long the turn took.

inputTokensinteger|null

What the provider counted on the way in.

cachedTokensinteger

How much of that the provider served from its cache.

contextTokensinteger

The size of the conversation as measured. The same number as inputTokens when it is available.

contextLimitinteger

The limit in force — your contextTokens, or the default.

contextPartsobject

{ system, tools, memory, messages }, scaled so they add up to the total.

contextClearedinteger

How many tool results have been emptied so far in this run.

creditsinteger

What this turn cost.

totalCreditsinteger

What the run has cost so far — turns and paid tools together.

A context gauge

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 }
};
# 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}
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

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

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

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.