Skip to content

feat(telemetry): multi-chunk response continuation cost stitching in route_attempt #8

Open

Description

@dezren39

Context & Background

In OpenCode upstream commit a3f7f42ac9 (feat(core): continue responses after output token limits (#53876)) and commit 044d8b7d2e (feat(ai): continue split Gemini responses (#55056)), OpenCode added automatic multi-turn continuations: when an LLM truncates output because it reaches its maximum generation limit (finish_reason: length or max_tokens), the core runner automatically requests a continuation to complete the generation seamlessly.

In our fork (developing-today/opencode), every LLM call produces a discrete record in route_attempt with tokens_input, tokens_output, tokens_reasoning, and cost_usd. When a response requires multiple continuation requests, logging each continuation as an isolated attempt creates fragmented records that disconnect the full cost of the assistant turn.

Feature Objective

Enhance route_attempt and our usage metrics store to stitch continuation requests together into a unified turn attempt:

  1. Link continuation attempts to the primary root request via a continuation_of or parent_attempt_id reference.
  2. Provide aggregated cost and token rollups for multi-segment responses so that total turn cost and duration are accurately reflected.

Technical Specifications

1. Schema Enhancement

  • Add a new migration in packages/core/src/database/migration/ (e.g. 20261017000000_route_attempt_continuation.ts):
    ALTER TABLE `route_attempt` ADD `continuation_of` integer;
    ALTER TABLE `route_attempt` ADD `continuation_index` integer DEFAULT 0;
    CREATE INDEX `route_attempt_continuation_idx` ON `route_attempt` (`continuation_of`);

2. Continuation Tracking in LLM Runner

  • In packages/core/src/session/runner/llm.ts:
    • When a completion finishes with finish_reason: length and initiates a continuation:
    • Pass the parent attempt ID to the subsequent continuation request.
    • Set continuation_index incrementally (0 for initial, 1 for first continuation, etc.).
    • Calculate cost_usd for the incremental tokens of each chunk.

3. Metrics Rollup Queries

  • In packages/core/src/model-usage.ts and packages/core/src/model-route-log/sql.ts:
    • When querying turn attempts, provide a helper getTurnAttemptMetrics(rootAttemptId) that sums:
      • Total input tokens (excluding duplicate cached prefixes where applicable)
      • Total output tokens across all continuation chunks
      • Total cost_usd
      • Total response time (from start of chunk 0 to end of last chunk)

Key Files & References

  • packages/core/src/session/runner/llm.ts: Output token limit detection, response continuation loop
  • packages/core/src/database/schema.gen.ts: route_attempt table
  • packages/core/src/model-usage.ts: Attempt metrics aggregation and reporting
  • packages/core/src/tool/plugin/route-usage.ts: route_usage inspection tool

Acceptance Criteria

  1. When an LLM response triggers automatic continuation, the follow-up requests record continuation_of pointing to the initial attempt.
  2. route_usage and session cost aggregations correctly tally all continuation chunks into the total turn cost.
  3. If a continuation fails, the initial partial output and failure reason are logged accurately.
  4. Unit tests in packages/core/test/ verify continuation linking and aggregated token/cost calculation.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions