Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-media-output-cap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---

Keep encoded image payload size from reducing the dynamic output token allowance.
5 changes: 3 additions & 2 deletions packages/opencode/src/kilocode/session/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ export namespace KiloLLM {
messages: ModelMessage[]
tools: Record<string, { description?: string; inputSchema?: unknown }>
configured: number | undefined
tokens?: number
contextTokens?: number
}): number | undefined {
if (input.configured == null) return input.configured
if (input.configured <= 0) return undefined
const { context } = input.model.limit
if (!context) return input.configured

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.

const available = context - tokens - SAFETY
// If available is ≤0 the input alone exceeds context — return the original
// value so the provider returns a natural overflow error which triggers
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/session/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const live: Layer.Layer<
messages: estimated,
tools: base.tools,
configured: base.params.maxOutputTokens,
tokens: usage?.raw,
contextTokens: usage?.normalized,
})
if (
preflight &&
Expand Down
24 changes: 24 additions & 0 deletions packages/opencode/test/kilocode/session-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ describe("Kilo request estimation", () => {
expect(KiloLLM.needsEstimate({ model: model({ context: 0, output: 32_000 }), configured: 32_000 })).toBe(false)
expect(KiloLLM.needsEstimate({ model: mdl, configured: 32_000 })).toBe(true)
})

test("caps output tokens from media-normalized context instead of encoded image bytes", () => {
const configured = 32_000
const messages = [
{
role: "user",
content: [{ type: "image", image: `data:image/png;base64,${"x".repeat(300_000)}` }],
},
] satisfies ModelMessage[]
const usage = KiloSessionOverflow.measure({ messages, tools: {} })
const mdl = model({ context: usage.raw + 2_048 + 1_758, output: configured })

expect(usage.raw).toBeGreaterThan(usage.normalized + configured)
expect(KiloLLM.capOutputTokens({ model: mdl, messages, tools: {}, configured })).toBe(configured)
})

test("still reduces output tokens for oversized text context", () => {
const configured = 32_000
const messages = [{ role: "user", content: "x".repeat(300_000) }] satisfies ModelMessage[]
const usage = KiloSessionOverflow.measure({ messages, tools: {} })
const mdl = model({ context: usage.normalized + 2_048 + 8_000, output: configured })

expect(KiloLLM.capOutputTokens({ model: mdl, messages, tools: {}, configured })).toBe(8_000)
})
})

describe("Kilo preflight compaction", () => {
Expand Down