Skip to main content
Every agent records its token usage; read the running total whenever you need it. Token accounting is on for every agent that runs through the native OpenAI path — the default when you pass a bare model name like "gpt-4o-mini". You don’t turn it on, you just read it back.

Quick Start

1

Run an agent and read the total

A bare Agent(llm="gpt-4o-mini") records its usage automatically. Read it back with the global collector:
The numbers are real spend — no metrics=True, no verbose, no display flag required.
2

Break usage down per agent

Pass a name to each agent and the collector rolls usage up under by_agent:

How It Works

Accounting happens inside the native OpenAIClient after every successful completion — one record per tool-loop iteration — and lands in the global TokenCollector.
Token accounting is on by default and does not depend on output="verbose", metrics=True, or any display flag. A quiet default agent still records real spend — display flags gate rendering only, never accounting.

Session Summary Shape

get_session_summary() returns a plain dict you can read or serialise:
Each metrics block carries input_tokens, output_tokens, cached_tokens, reasoning_tokens, audio_input_tokens, and audio_output_tokens.

Common Patterns

Reset before, read the summary after — you get the spend for just this run:
by_agent gives a per-agent rollup so you can attribute spend to the right worker:
When an agent calls a tool and then completes, each model call is its own paid iteration. A single tool-using turn therefore records total_interactions == 2 — one for the tool-call step and one for the final answer — and both are summed into the totals.

Latest Call Only

For CLI or Harbor envelopes that need the most recent completion’s usage — not the session total — read OpenAIClient.last_token_metrics. It exposes the latest completion’s TokenMetrics and is cleared when a response carries no usable usage.

Gateway

Unified control plane for agents, tools, and delivery.

Observability

Trace and monitor agent runs end to end.

Output Config

Control what an agent renders vs what it records.