Skip to content

[FEATURE] Emit a session token/cost summary on agent stream completion #552

Closed

Description

@edenreich

Problem

Consumers of the JSON-line stream from infer agent (notably inference-gateway/infer-action, which surfaces run results on the GitHub issue) want to report how much a run cost.

Today the stream exposes per-turn token counts: each assistant ConversationMessage carries token_usage (prompt_tokens / completion_tokens / total_tokens) — see cmd/agent.go processSyncResponse → outputMessage. infer-action now sums these and shows a token line in the result footer.

But the dollar cost is never emitted on the stream. The CLI already computes it internally for the interactive UI via the pricing service (internal/services/pricing_service.go CalculateCost) and SessionCostStats (internal/domain/pricing.go). Stream consumers can't reach it without re-implementing the per-model pricing table, which would drift.

Proposal

When an infer agent session completes, emit a single structured status line (the machinery already exists — AgentSession.outputStatusMessage emits {type, message, timestamp, ...metadata}), e.g.:

{
  "type": "session_stats",
  "message": "Session complete",
  "model": "deepseek/deepseek-v4-flash",
  "prompt_tokens": 21000,
  "completion_tokens": 1260,
  "total_tokens": 22260,
  "requests": 7,
  "cost": { "input": 0.0021, "output": 0.0008, "total": 0.0029, "currency": "USD" }
}

Sourced from the same SessionTokenStats / SessionCostStats the /cost shortcut already displays. This lets headless consumers print real cost without summing per-turn usage or duplicating pricing.

Notes / questions

  • A final aggregate line is preferable to per-turn cost, but per-turn cost on each assistant message would also work.
  • Should this be gated (e.g. only in non-interactive agent mode, or behind a flag) to avoid noise for existing stream consumers? A new type is additive and should be safe to ignore.
  • Once available, infer-action will replace its token-only footer with the reported cost.

Filed from infer-action work on richer run reporting.

Activity

  1. added
    enhancement
    on May 29, 2026
  2. added a commit that references this issue on May 29, 2026
    892203b
  3. inference-gateway-releaser commented on May 29, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.115.0 🎉

    The release is available on GitHub release

    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

    enhancementreleased

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions