Repository navigation
feat(telemetry): multi-chunk response continuation cost stitching in route_attempt #8
Description
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:
- Link continuation attempts to the primary root request via a
continuation_oforparent_attempt_idreference. - 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: lengthand initiates a continuation: - Pass the parent attempt ID to the subsequent continuation request.
- Set
continuation_indexincrementally (0 for initial, 1 for first continuation, etc.). - Calculate
cost_usdfor the incremental tokens of each chunk.
- When a completion finishes with
3. Metrics Rollup Queries
- In
packages/core/src/model-usage.tsandpackages/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)
- When querying turn attempts, provide a helper
Key Files & References
packages/core/src/session/runner/llm.ts: Output token limit detection, response continuation looppackages/core/src/database/schema.gen.ts:route_attempttablepackages/core/src/model-usage.ts: Attempt metrics aggregation and reportingpackages/core/src/tool/plugin/route-usage.ts:route_usageinspection tool
Acceptance Criteria
- When an LLM response triggers automatic continuation, the follow-up requests record
continuation_ofpointing to the initial attempt. route_usageand session cost aggregations correctly tally all continuation chunks into the total turn cost.- If a continuation fails, the initial partial output and failure reason are logged accurately.
- Unit tests in
packages/core/test/verify continuation linking and aggregated token/cost calculation.
Activity