feat: capture token usage from AI providers#877
Merged
Conversation
Every AI provider in ai-http-client already parses token usage from
API responses (prompt_tokens, completion_tokens, total_tokens), but
AIConversationLoop dropped this data on the floor — it only read
content and tool_calls from the response.
Changes:
- AIConversationLoop accumulates usage across all turns in a
conversation and includes it in the return value
- AIStep writes token_usage to job engine_data after loop completion
- ChatOrchestrator stores token_usage in session metadata, accumulating
across multi-turn chat conversations (processChat, processContinue,
processPing all covered)
The data flows:
Provider response → AIConversationLoop (accumulate) →
AIStep → job engine_data.token_usage
ChatOrchestrator → session metadata.token_usage
No new tables or schema changes. Usage is stored in existing JSON
fields (engine_data on jobs, metadata on chat sessions).
Closes #849.
Homeboy Results —
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stops dropping token usage data on the floor. Every AI provider in
ai-http-clientalready parsesprompt_tokens,completion_tokens, andtotal_tokensfrom API responses — Data Machine was just ignoring them.The data flow
Changes (3 files, 75 lines added)
AIConversationLoop.phpusagefrom$ai_response['data']['usage']across all turns. Addsusageto return value (including error returns).AIStep.phptoken_usageto jobengine_datavia read-then-merge pattern.ChatOrchestrator.phptoken_usagein session metadata. Accumulates across multi-turn conversations. All three entry points covered:processChat,processContinue,processPing.Storage format
Jobs (
engine_dataJSON):{ "task_type": "...", "token_usage": { "prompt_tokens": 2450, "completion_tokens": 380, "total_tokens": 2830 } }Chat sessions (
metadataJSON):{ "status": "completed", "token_usage": { "prompt_tokens": 15200, "completion_tokens": 3400, "total_tokens": 18600 } }Testing
php -lFuture work
Closes #849.