Skip to content

Use media-normalized context for output token capping - #11497

Closed
Bortlesboat wants to merge 6 commits into
Kilo-Org:mainfrom
Bortlesboat:fix/media-normalized-output-cap
Closed

Use media-normalized context for output token capping#11497
Bortlesboat wants to merge 6 commits into
Kilo-Org:mainfrom
Bortlesboat:fix/media-normalized-output-cap

Conversation

@Bortlesboat

Copy link
Copy Markdown

Summary

  • Use the media-normalized request estimate when capping maxOutputTokens, so base64 image transport size no longer consumes the output allowance.
  • Keep raw media accounting available through KiloSessionOverflow.measure().raw for payload-size recovery paths.
  • Add regressions for image-heavy requests and text-heavy requests.

Fixes #11432

Tests

  • bun test ./test/kilocode/session-overflow.test.ts
  • bun run typecheck from packages/opencode
  • bun run script/check-opencode-annotations.ts
  • bun x prettier --check .changeset/fix-media-output-cap.md packages/opencode/src/kilocode/session/llm.ts packages/opencode/src/session/llm.ts packages/opencode/test/kilocode/session-overflow.test.ts
  • git diff --check

Note: root bun typecheck reaches packages/kilo-jetbrains and fails locally because Gradle requires JVM 17 while this machine is configured with JVM 8.


const tokens = input.tokens ?? KiloSessionOverflow.measure({ messages: input.messages, tools: input.tools }).raw
const tokens =
input.contextTokens ?? KiloSessionOverflow.measure({ messages: input.messages, tools: input.tools }).normalized

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: normalized removes almost all image context cost from the output-cap calculation

KiloSessionOverflow.measure() turns every file/image payload into the fixed [encoded media] marker before producing normalized, so this cap now treats even several large images as nearly free context. On smaller multimodal windows that can leave maxOutputTokens effectively uncapped even though the provider still has to spend real prompt tokens on the images, which turns a preventable cap adjustment into a provider-side context overflow. A media-aware estimate, or at least a non-trivial per-image reserve here, would keep the cap from over-allocating.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@kilo-code-bot

kilo-code-bot Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

The previously flagged WARNING (packages/opencode/src/kilocode/session/llm.ts around the output-cap calculation) is resolved: the latest commit adds a contextMedia parameter alongside contextTokens, and the sole call site in packages/opencode/src/session/llm.ts now passes both from the same measure() call, so the media reserve (media * MEDIA_OUTPUT_RESERVE) is applied on the main request path instead of being short-circuited. New regression tests cover the precomputed-both-fields path.

Files Reviewed (3 files)
  • packages/opencode/src/kilocode/session/llm.ts
  • packages/opencode/src/session/llm.ts
  • packages/opencode/test/kilocode/session-overflow.test.ts
Previous Review Summaries (2 snapshots, latest commit 2f3b28f)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 2f3b28f)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/kilocode/session/llm.ts 63 contextTokens still short-circuits the new media reserve, so the main request path continues to cap output from usage.normalized alone and can over-allocate maxOutputTokens for multimodal requests.
Files Reviewed (3 files)
  • packages/opencode/src/kilocode/session/llm.ts - 1 issue
  • packages/opencode/src/kilocode/session/overflow.ts - 0 issues
  • packages/opencode/test/kilocode/session-overflow.test.ts - 0 issues

Previous review (commit 3a1a722)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/kilocode/session/llm.ts 62 Using normalized here removes nearly all per-image context cost, so multimodal requests near the model limit can still over-allocate maxOutputTokens and fail with provider-side context overflows.
Files Reviewed (4 files)
  • .changeset/fix-media-output-cap.md - 0 issues
  • packages/opencode/src/kilocode/session/llm.ts - 1 issue
  • packages/opencode/src/session/llm.ts - 0 issues
  • packages/opencode/test/kilocode/session-overflow.test.ts - 0 issues

Reviewed by claude-sonnet-5-20260630 · Input: 38 · Output: 9.3K · Cached: 1.3M

Review guidance: REVIEW.md from base branch main

@Bortlesboat

Copy link
Copy Markdown
Author

Updated in 2f3b28ff0 to reserve media context for output-token capping without falling back to raw encoded-byte size.

Verification:

  • bun test test/kilocode/session-overflow.test.ts -> 24/24 passed
  • bun run typecheck -> passed

Note: local push hook required Bun 1.3.14; this machine has 1.3.10, so I bypassed the local hook after the focused tests/typecheck passed.

@johnnyeric
johnnyeric requested a review from chrarnoldus July 1, 2026 12:11
@chrarnoldus chrarnoldus self-assigned this Jul 2, 2026
…d-output-cap

# Conflicts:
#	packages/opencode/src/kilocode/session/llm.ts
#	packages/opencode/src/session/llm.ts
#	packages/opencode/test/kilocode/session-overflow.test.ts
@chrarnoldus

Copy link
Copy Markdown
Collaborator

superseded by #11891

@chrarnoldus chrarnoldus closed this Jul 6, 2026
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.

Encoded images can collapse the dynamic output token allowance

2 participants