Skip to content

Conversation

@ahmedhesham6
Copy link
Contributor

Proposes standardized tracking of token usage, cost estimation, and context window status across ACP implementations.

  • Token usage reported in PromptResponse (per-turn data)
  • Context window and cost reported in session/status (session state)

Proposes standardized tracking of token usage, cost estimation, and
context window status across ACP implementations.

- Token usage reported in PromptResponse (per-turn data)
- Context window and cost reported in session/status (session state)
@ahmedhesham6 ahmedhesham6 requested a review from a team as a code owner December 7, 2025 00:51
Comment on lines 110 to 111
"percentage": 26.5,
"remaining": 147000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these two fields needed, they are fully derivable from size and used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They’re redundant mathematically, but they carry the agent’s own calculation/rounding and simplify client work

Comment on lines 125 to 126
- `percentage` (number, required) - Percentage used (0-100)
- `remaining` (number, required) - Tokens remaining

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on the derived fields from size and used are these fields needed


### Design Principles

1. **Separation of concerns** - Token usage is per-turn data, context window and cost are session state

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how will this work with subagent "tools" that aren't performing full turns but are actively updating token usage frequently

@benbrandt
Copy link
Member

@josevalim @SteffenDE I'd love to get your input on this one since you were looking into this a bit

@SteffenDE
Copy link
Contributor

We like the idea and we definitely want to have a way to do this in ACP! For us, the most important part is the current usage (in percent). Including a usage in prompt responses feels like a no-brainer, but since an ACP prompt often consists of multiple agent turns, wrappers like Claude-Code-ACP would need to accumulate the different tokens from the turns. I think that's alright though. As mentioned in #316 (comment), one needs to be careful about usage data from subagents, as those should not be included, or optionally provided separately.

The PR proposes a new session/status method. I'm not sure if agents like Claude Code have proper APIs to query the current status at any time, so a different idea would be to only send the current usage information (current tokens, max tokens, percent) as part of session/update notifications only. An agent that supports getting the current usage without a prompt may then immediately send the update when creating a new chat, resuming a chat, forking a chat, etc., similar to how the available command updates are sent. An agent that only provides usage when actively prompting could only start sending the updates after sending a new prompt. That might mean that when resuming a chat, the client UI cannot immediately show the usage, but it allows more flexibility for agents.

@ahmedhesham6
Copy link
Contributor Author

ahmedhesham6 commented Dec 11, 2025

@SteffenDE Thanks a lot for the detailed explanation and context 🙏
Just to confirm I understood you correctly — is this roughly what you’re suggesting for the session/update approach?

{
  "jsonrpc": "2.0",
  "method": "session/update",
  "params": {
    "sessionId": "sess_abc123",
    "update": {
      "sessionUpdate": "context",
      "used": 53000,
      "size": 200000,
      "percentage": 26.5
    }
  }
}

If this matches what you had in mind, I can adjust the RFD in that direction.

@SteffenDE
Copy link
Contributor

@ahmedhesham6 yes! I'd wait before changing things though, since I'm not a maintainer here and basically just stating my opinion :D

@ahmedhesham6
Copy link
Contributor Author

What do you think @benbrandt?

@benbrandt
Copy link
Member

Yeah I think something simple to start would be great. As @SteffenDE mentioned, support for this will likely vary wildly (and we've also seen mixed support of even these basic metrics within the same agent lol)

I think we should let this be driven by the agent, as they will likely get the information from the provider and may forward it, but might not hold on to it. Requiring them to have the data at all points might be too much... So I'd opt for a simple way to report the basic information we feel we need, and go from there

…cations

Refines the tracking of context window and cost information by transitioning from `session/status` requests to `session/update` notifications. This change allows agents to proactively push updates, enhancing flexibility and real-time data availability for clients. The `cost` field is now optional, and the `remaining` field has been removed, as clients can compute it from `size` and `used`. Updated documentation to reflect these changes and provide clearer usage patterns.
- `total_tokens` (number, required) - Sum of all token types across session
- `input_tokens` (number, required) - Total input tokens across all turns
- `output_tokens` (number, required) - Total output tokens across all turns
- `reasoning_tokens` (number, optional) - Total reasoning tokens (for o1/o3 models)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since in ACP we usually refer to this as thought I wonder if we could align that?


- `used` (number, required) - Tokens currently in context
- `size` (number, required) - Total context window size in tokens
- `percentage` (number, required) - Percentage used (0-100)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we send this just to save a calculation on the client? since we allow them to calculate remaining, maybe we let them do this too?


#### Cost Fields (optional)

- `cost` (object, optional) - Cumulative session cost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nitpick: it seems weird that this is part of a "context" update.
I wonder if all of this is just usage from a conceptual point of view?
And roughly the same data can be sent at the end of the turn, with mid-turn updates? So kind of merge these?

It seems you want to distinguish between turn usage vs total usage. Which makes sense, but I wonder if we can distinguish then between turn vs session usage?

- Agent knows exact context window size (varies by model)
- Agent knows how it counts tokens (different tokenizers)
- Agent knows about special tokens, system messages, etc.
- Client can still recalculate if needed (all raw data provided)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont' really understand these arguments... Unless percentage will somehow be different than used / total (which would be weird) I feel the agent gets all of this by controlling the token counts already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants