# Token tracking

Each assistant message carries one [`Usage`](https://github.com/Fzkuji/OpenProgram/blob/main/openprogram/models/providers/types.py) record.

| Field | Meaning |
| --- | --- |
| `input` | Non-cached input. Excludes cached tokens, as Anthropic does; inclusive vendors are converted by subtraction. |
| `output` | Output tokens |
| `cache_read` | Served from the prompt cache |
| `cache_write` | Written to the prompt cache (explicit caching only) |
| `total_tokens` | Vendor-reported total |
| `cost` | USD, from the enabled model's per-million prices |

## What each protocol reports

Taken from the vendor's final event or last chunk.

| Protocol | Providers | Raw fields | Cache |
| --- | --- | --- | --- |
| Anthropic Messages | `anthropic`, gateways such as `minimax`, `kimi_coding`, `vercel_ai_gateway` | `input_tokens` `output_tokens` `cache_read_input_tokens` `cache_creation_input_tokens` | read, write |
| OpenAI Responses | `openai` `openai_codex` `azure_openai_responses` `github_copilot` | `input_tokens` (cached subtracted) `output_tokens` `input_tokens_details.cached_tokens` `total_tokens` | read |
| OpenAI Completions | `deepseek` `groq` `mistral` `openrouter`, other compatible endpoints | `prompt_tokens` `completion_tokens` (`completion_tokens_details` reasoning split out) `total_tokens` | none |
| Google Generative AI | `google` | `prompt_token_count` `candidates_token_count` `total_token_count` | none |
| Cloud Code Assist | `gemini_subscription` `google_gemini_cli` | `promptTokenCount` `candidatesTokenCount` `thoughtsTokenCount` `cachedContentTokenCount` `totalTokenCount` | read |
| Bedrock Converse Stream | `amazon_bedrock` | `inputTokens` `outputTokens` `cacheReadInputTokens` `cacheWriteInputTokens` `totalTokens` | read, write |

Cost is computed right after extraction. On OpenAI Responses the returned `service_tier` adjusts it, so [Fast](https://fzkuji.github.io/OpenProgram/docs/models/fast-tier.html) requests cost the priority rate.

## Where it shows

| Place | Shows |
| --- | --- |
| Each message | Its own usage and cost, saved with the session |
| Chat badge | Latest call only, such as `11.2k in · 450 out`; that input count is what the context occupies. Tooltip: base, cache write, cache hit for Claude-protocol; base, cached for Codex; a percent cached otherwise. `1.2k` above a thousand, `1.0m` above a million. |
| Context bar | Fill percent, from the `context_stats` WebSocket event after each turn |
| Function card | Per execution, from the function's own runtime, independent of the chat numbers |

### Context panel

Click the ring. Before a new chat's first message it shows what that message would carry, against the selected model's window.

| Part | Lists |
| --- | --- |
| System prompt | Base prompt, environment, AGENTS.md, skills index and so on |
| Tools | Loaded, on demand, MCP |
| Memory | Each layer's `MEMORY.md` |
| Conversation | By kind, with its largest messages |

Click a part or its bar segment to open it, a row to read the text sent, a message to jump to it. **Compact conversation** compacts now; refused during a turn, and no turn starts until it ends.
