Skip to content

feat: capture token usage from AI providers#877

Merged
chubes4 merged 1 commit intomainfrom
feature/849-token-usage-tracking
Mar 19, 2026
Merged

feat: capture token usage from AI providers#877
chubes4 merged 1 commit intomainfrom
feature/849-token-usage-tracking

Conversation

@chubes4
Copy link
Member

@chubes4 chubes4 commented Mar 19, 2026

Summary

Stops dropping token usage data on the floor. Every AI provider in ai-http-client already parses prompt_tokens, completion_tokens, and total_tokens from API responses — Data Machine was just ignoring them.

The data flow

ai-http-client                     Data Machine
┌──────────────┐     ┌────────────────────────────────────────────┐
│ Provider      │     │ AIConversationLoop                         │
│ format_       │────▶│ accumulates usage across turns              │
│ response()    │     │                                            │
│ returns usage │     │     ┌──────────────┐  ┌──────────────────┐ │
│               │     │     │ AIStep       │  │ ChatOrchestrator │ │
│               │     │     │ → engine_data│  │ → metadata       │ │
│               │     │     │   .token_    │  │   .token_usage   │ │
│               │     │     │    usage     │  │                  │ │
└──────────────┘     │     └──────────────┘  └──────────────────┘ │
                      └────────────────────────────────────────────┘

Changes (3 files, 75 lines added)

File What changed
AIConversationLoop.php Accumulates usage from $ai_response['data']['usage'] across all turns. Adds usage to return value (including error returns).
AIStep.php After loop completes, writes token_usage to job engine_data via read-then-merge pattern.
ChatOrchestrator.php Stores token_usage in session metadata. Accumulates across multi-turn conversations. All three entry points covered: processChat, processContinue, processPing.

Storage format

Jobs (engine_data JSON):

{
  "task_type": "...",
  "token_usage": {
    "prompt_tokens": 2450,
    "completion_tokens": 380,
    "total_tokens": 2830
  }
}

Chat sessions (metadata JSON):

{
  "status": "completed",
  "token_usage": {
    "prompt_tokens": 15200,
    "completion_tokens": 3400,
    "total_tokens": 18600
  }
}

Testing

  • All 3 files pass php -l
  • PHPUnit: 906 tests, same pass/fail as main
  • No schema changes — uses existing JSON fields

Future work

Closes #849.

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.
@chubes4 chubes4 merged commit 440d874 into main Mar 19, 2026
2 of 3 checks passed
@chubes4 chubes4 deleted the feature/849-token-usage-tracking branch March 19, 2026 03:27
@github-actions
Copy link

github-actions bot commented Mar 19, 2026

Homeboy Results — data-machine

Lint

Failure Digest

Lint Failure Digest

Autofixability classification

  • Overall: human_needed
  • Autofix enabled: no
  • Autofix attempted this run: no
  • Human-needed failed commands:
    • lint
  • Failed commands with available automated fixes:
    • lint
  • Automated fixes are disabled for this step. Commands with available fix support in this run: lint

Machine-readable artifacts

  • homeboy-lint-summary.json
  • homeboy-test-failures.json
  • homeboy-audit-summary.json
  • homeboy-autofixability.json

⚡ Scope: changed files only

lint (changed files only)

Test

⚡ Scope: changed files only

test (changed files only)

Audit

⚡ Scope: changed files only

audit (changed files only)

Tooling versions
  • Homeboy CLI: homeboy 0.81.1+81c8f58a
  • Extension: wordpress from https://github.com/Extra-Chill/homeboy-extensions
  • Extension revision: unknown
  • Action: Extra-Chill/homeboy-action@v1

Homeboy Action v1

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.

Token/usage tracking primitive for chat sessions

1 participant