mirror of
https://gitee.com/zhangsken/opencode.git
synced 2026-10-16 10:30:01 +08:00
feat(core): cap requested output tokens at 256k (#51962)
This commit is contained in:
@@ -46,6 +46,8 @@ const IMAGE_REMOVED =
|
||||
const GENERATION_KEYS = new Set(Object.keys(GenerationOptions.fields))
|
||||
// Used when the catalog has no output limit for the model.
|
||||
const OUTPUT_TOKEN_FALLBACK = 32_000
|
||||
// No reply needs more, however much the model allows.
|
||||
const OUTPUT_TOKEN_MAX = 256_000
|
||||
// A summary never needs more, and a request asking for more cannot be shrunk to fit a window the catalog overstates.
|
||||
const SUMMARY_OUTPUT_MAX = 32_000
|
||||
// Prompt text is estimated at about 4 characters per token, which can run low on dense text such as code.
|
||||
@@ -87,7 +89,7 @@ const outputLimit = (
|
||||
kind: "primary" | "compaction",
|
||||
inputTokens?: Input["inputTokens"],
|
||||
) => {
|
||||
const model = limit.output > 0 ? limit.output : OUTPUT_TOKEN_FALLBACK
|
||||
const model = Math.min(limit.output > 0 ? limit.output : OUTPUT_TOKEN_FALLBACK, OUTPUT_TOKEN_MAX)
|
||||
const requested = kind === "compaction" ? Math.min(model, SUMMARY_OUTPUT_MAX) : model
|
||||
if (inputTokens === undefined || limit.context <= 0) return requested
|
||||
const room = limit.context - inputTokens.measured - Math.ceil(inputTokens.estimated * (1 + ESTIMATE_ERROR))
|
||||
|
||||
@@ -128,6 +128,7 @@ const undersizedContextModel = testModel("undersized-context", { context: 1, out
|
||||
const recoveryModel = testModel("recovery", { context: 200_000, output: 1_000 })
|
||||
const fittedOutputModel = testModel("fitted-output", { context: 100_000, output: 64_000 })
|
||||
const smallWindowModel = testModel("small-window", { context: 64_000, output: 16_000 })
|
||||
const largeOutputModel = testModel("large-output", { context: 1_000_000, output: 1_000_000 })
|
||||
|
||||
test("calculates step cost using the matching context tier", () => {
|
||||
expect(
|
||||
@@ -3424,6 +3425,14 @@ describe("SessionRunnerLLM", () => {
|
||||
expect(s.requests[1]?.generation?.maxTokens).toBeGreaterThan(100_000 - 50_000 - 200)
|
||||
})
|
||||
|
||||
scenario("caps the output limit a large model advertises", function* (s) {
|
||||
s.currentModel = largeOutputModel
|
||||
yield* s.llm.push(TestLLM.text("Answer", "text-large-output"))
|
||||
yield* s.runPrompt("Question")
|
||||
|
||||
expect(s.requests[0]?.generation?.maxTokens).toBe(256_000)
|
||||
})
|
||||
|
||||
scenario("gives the summary its full output limit when the conversation overshot the threshold", function* (s) {
|
||||
// The conversation overshot the threshold, so the prepared summary request exceeds the budget and `deliver`
|
||||
// shrinks it before sending. The output limit must follow the budget, not the oversized prepared request.
|
||||
|
||||
Reference in New Issue
Block a user