Skip to content

[REFACTOR] Unify chat and headless token accumulation #835

Closed

Description

@edenreich

Problem

Two independent token accumulators read the same response.Usage:

  • Chat funnels through AgentServiceImpl.storeIterationMetrics (internal/agent/agent.go:724), storing domain.ChatMetrics per request with a tokenizer polyfill when providers omit usage.
  • Headless re-implements inline counters (cmd/agent.go:108-109, accumulation at :679-682, plus its own AddTokenUsage at :708).

Cost math can drift between modes, and any metrics recorder must tap two places.

Design

Extract one shared usage accumulator (likely alongside domain.ChatMetrics) that both AgentServiceImpl and the headless executor feed; the headless executor drops its ad-hoc fields. Keep the tokenizer polyfill in the shared path so both modes estimate identically when usage is missing from the stream.

Acceptance criteria

  • Single accumulation code path; for identical mock scenarios, headless totals equal chat totals (assertable via the existing usage-report scenario, 142 tokens)
  • /cost and the headless final stats read from the same source

Dependencies

None. Prerequisite for the metrics recorder issue.

Activity

  1. added
    refactor
    on Jul 10, 2026
  2. added theissue type on Jul 10, 2026
  3. moved this to Todo in 2026on Jul 10, 2026
  4. moved this from Todo to QA in 2026on Jul 11, 2026
  5. added a commit that references this issue on Jul 11, 2026
    95cd551
  6. moved this from QA to Done in 2026on Jul 11, 2026
  7. inference-gateway-releaser commented on Jul 11, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.142.2 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorreleased

    Type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions