Repository navigation
Fix #14: report Anthropic thinking token usage - #44
Conversation
Completion Summary
aidevops.sh v3.32.317 plugin for OpenCode v1.18.30 with gpt-5.6-sol spent 30m and 251,204 tokens on this with the user in an interactive session. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
🟢 Approval recommended
The change is localized to usage parsing/accumulation and aligns with the PR’s documented intent without introducing obvious behavioral regressions.
Pull request overview
This PR updates the Anthropic text-generation model to surface Anthropic “thinking tokens” as the SDK’s TokenUsage::$thoughtTokens, while keeping output_tokens as the overall completion/output total and correctly accumulating usage across pause_turn continuation legs.
Changes:
- Parse
usage.output_tokens_details.thinking_tokensfrom Anthropic responses and map it toTokenUsage::$thoughtTokens. - Accumulate
thinking_tokensacrosspause_turncontinuation legs (similar to existing accumulation for input/output/cache token counts). - Keep
thoughtTokensasnullwhen the Anthropic response omits the breakdown.
File summaries
| File | Description |
|---|---|
src/Models/AnthropicTextGenerationModel.php |
Accumulates and maps Anthropic thinking_tokens into TokenUsage as thoughtTokens, including across paused-turn continuations. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
## What? Fixes #41 Keeps Anthropic's `usage.server_tool_use` (for example `web_search_requests`) in the result's additional data, so callers can tell whether a web search ran and how many were billed. ## Why? Anthropic bills web search per request, separately from tokens. Today the count is lost in two places: 1. `parseResponseDataToGenerativeAiResult()` unsets the whole `usage` key from the additional data. `TokenUsage` keeps the token counts, but `server_tool_use` is not token data and goes with it. 2. The paused-turn loop in `generateTextResult()` rebuilds `usage` from the four token counts only. So even with (1) fixed, a paused web-search turn, which is the common case for search, would still lose the count. ## How? - The paused-turn loop sums every integer counter in `usage.server_tool_use` across legs, using the same pattern as the token accumulation. The key is only added when the API reported it. - The parser copies `usage.server_tool_use` into the additional data as `server_tool_use`, next to `stop_reason`. - `UsageData` gains `server_tool_use?: array<string, int>`. The `server_tool_use` / `web_search_tool_result` content blocks are still skipped, as before. The counter is enough for pricing and reporting, and a provider-agnostic field on `GenerativeAiResult` would be a php-ai-client decision, as the issue notes. ## Testing Instructions Adds `tests/Models/AnthropicTextGenerationModelTest.php`, which drives `generateTextResult()` through a stub `HttpTransporterInterface`: - one response with `web_search_requests: 1` reports `['web_search_requests' => 1]` - a `pause_turn` leg followed by an `end_turn` leg reports `2`, with token usage still summed - a response without `server_tool_use` adds no key The first two fail on `trunk` and pass with this change. PHPUnit and PHPStan pass locally (PHP 8.4). PHPCS reports no new violations, only the ones already on `trunk` (unused `AiClient` import and the container block indentation, which #44 also fixes). To check against the API: run a prompt with `usingWebSearch()` and read `$result->getAdditionalData()['server_tool_use']`. ## Changelog Entry > Fixed - Report server tool usage, such as web search request counts, in the result's additional data. <details> <summary>Use of AI Tools</summary> AI assistance: Yes Tool(s): Claude Code Model(s): Claude Opus 5.5 Used for: Code and tests. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: muneeb-ashraf <muneebashraf@git.wordpress.org> Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org> Co-authored-by: webandseo <webandseofr@git.wordpress.org>
Summary
usage.output_tokens_details.thinking_tokenstoTokenUsage::$thoughtTokens.output_tokensas the completion total because Anthropic defines thinking tokens as a billed subset of output tokens.pause_turncontinuation legs.nullwhen an older response omits the breakdown.Anthropic's current documentation identifies
usage.output_tokens_details.thinking_tokensas the authoritative number of billed internal-reasoning tokens. The Messages API exposes the breakdown without a beta header:This matches the existing Google and OpenAI provider convention: completion tokens include thinking/reasoning, while
thoughtTokensreports that subset separately.Files Changed
src/Models/AnthropicTextGenerationModel.phpRuntime Testing
trunkretains its unrelated pre-existing metadata warning.Resolves #14
aidevops.sh v3.32.317 plugin for OpenCode v1.18.30 with gpt-5.6-sol spent 30m and 251,204 tokens on this with the user in an interactive session.