Skip to content

fix(ai): clamp OpenAI Responses max_output_tokens to model capacity - #128282

Merged
obviyus merged 4 commits into
openclaw:mainfrom
Parker-Fawcett:fix/openai-responses-max-tokens-clamp
Oct 14, 2026
Merged

obviyus merged 4 commits into
openclaw:mainfrom
Parker-Fawcett:fix/openai-responses-max-tokens-clamp

Conversation

@Parker-Fawcett

@Parker-Fawcett Parker-Fawcett commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #127560

What Problem This Solves

An explicitly configured output budget larger than the selected model's catalog capacity reaches strict Responses endpoints unchanged. They reject an otherwise valid agent turn with HTTP 400.

Why This Change Was Made

Both Responses request owners now reuse clampMaxTokensToModel after applying the existing 16-token minimum. This preserves explicit limits when custom-model capacity is unknown, fixes the previous simple-path NaN regression, and retains managed zero/undefined fallback behavior. No caller-side workaround or new configuration is introduced.

Ported @Parker-Fawcett's contributor commits onto current request construction; thanks to the contributor and the reporter of #127560. Consolidated coverage around the two actual request owners instead of duplicating provider cases. No overlap with Pash/Sarah changes.

User Impact

Oversized budgets are capped at the model's declared output capacity. In-range budgets and unknown-capacity explicit budgets are unchanged. OpenAI and Azure Responses share these owners. Existing Codex unsupported-field stripping is unchanged.

Evidence

Built current main (653003a010987ad75393bdd0ff067283f41a1be7) and candidate in a secretless Node 24 Docker container. Drove the shipped openclaw --profile <isolated-profile> agent --local --agent main --message ... --thinking off --json entry point with an openai-responses provider and an actual local HTTP recording endpoint. The endpoint rejects output budgets above the model's configured catalog capacity of 128 and otherwise returns a normal Responses SSE completion.

Captured results:

base: configured=256; POST /v1/responses max_output_tokens=256
      HTTP 400 invalid_request_error: max_output_tokens exceeds model cap 128
      CLI exit=1; no completed answer
head: configured=256; POST /v1/responses max_output_tokens=128
      HTTP 200; CLI exit=0; finalAssistantVisibleText=CAP_OK
control base/head: configured=64; max_output_tokens=64; CLI exit=0; CAP_OK

Focused regression: pre-fix oversized case fails (expected 256 to be 128); candidate's three selected suites pass (76 tests). Cases protect the upper cap, unchanged in-range budget, 16-token floor, unknown capacity, and managed zero/undefined fallback. Changed-file oxfmt and oxlint pass.

Production +4/-2 (net +2: imports for the existing helper); tests +47/-0 against the merge base. No docs/configuration contract changes; changelog remains release-owned. Final revision only corrects the unknown-capacity test fixture typing; the six regression cases pass again and production/runtime proof inputs are unchanged.
Authenticated live gate on the same candidate: openai/gpt-5.4-mini, requested max_output_tokens=128, CLI exit 0, final answer LIVE_OK. Credentials stayed in a temporary host-only 0600 file behind a request-forwarding proxy and were deleted afterward; none entered the container. The first proxy setup was blocked by the private-network guard; explicitly allowing this operator-controlled test endpoint fixed the setup, without product changes.

Two-way door: a revert fully restores prior request shaping; no persisted-state or schema changes. Blast radius is output-budget shaping for OpenAI-compatible and Azure Responses requests.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@Parker-Fawcett
Parker-Fawcett marked this pull request as ready for review August 23, 2026 16:36
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 23, 2026
@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR caps OpenAI and Azure Responses output budgets at the selected model’s known capacity using the existing shared helper.

Example: An agent requests 256 output tokens from a model with a declared capacity of 128.

  • Before: The request sends max_output_tokens=256 and the strict endpoint rejects it with HTTP 400.
  • After: The request sends max_output_tokens=128 and the CLI completes with CAP_OK.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, correct repair with relevant shipped CLI and live-provider evidence; historical findings are resolved.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The supplied shipped CLI trace in isolated Node 24 Docker exercises the managed Responses request owner through real HTTP: baseline rejects 256 tokens, candidate sends 128 and completes, and the 64-token control is preserved; an authenticated OpenAI completion and owner-boundary regressions support the result, and the final test-only revision leaves production code unchanged.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: An output budget above model capacity makes strict Responses endpoints reject an otherwise valid agent turn.
Reason: The canonical report establishes the capacity contract, and both affected request owners are repaired without adding configuration or public API.

Merge readiness

✅ Ready for maintainer review

Keep this PR open: it repairs a current Responses request bug, preserves the established unknown-capacity behavior, and has sufficient runtime evidence; no actionable patch defects remain.

Priority: P2
Reviewed head: ef9d3097980136811d1636010ee9861affa2d335

Before merge

None.

Findings

None.

Agent review details

How this fits together

Managed transports and direct provider adapters combine caller output budgets with model metadata to construct requests sent to OpenAI or Azure Responses endpoints.

flowchart TD
  A[Caller output budget] --> C[Responses request owners]
  B[Model catalog capacity] --> C
  C --> D[Shared token clamp]
  D --> E[Responses HTTP request]
  E --> F[OpenAI or Azure endpoint]
Loading

Technical review

Best possible solution:

Land the shared-helper repair at both existing Responses request owners after the normal exact-head gates pass.

Do we have a high-confidence way to reproduce the issue?

Current-main source forwards 256 tokens for a model capped at 128, and the supplied CLI trace confirms the resulting rejection; this read-only review executed no target code.

Is this the best way to solve the issue?

Reusing the existing clamp at both request owners fixes the defect without caller workarounds while preserving unknown-capacity requests and managed fallback semantics.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 46053391a1ad.

Provenance checked

  • Direct Responses minimum output budget keeps the original intent (03d287d: Normalize requested budgets below 16 because Responses rejects them.)
  • Managed Responses minimum output budget keeps the original intent (acf67fb: Apply the existing adapter minimum at the managed request owner while leaving the upper-cap defect separate.)
  • Unknown custom-model capacity keeps the original intent (5720083: Preserve explicit requested limits when model metadata omits its output ceiling.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit acf67fb adds packages/ai/src/transports/openai-responses-params-internal.ts:322 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: acf67fbdcc6e; files: packages/ai/src/transports/openai-responses-params-internal.ts)
  • Vincent Koc: Raw commit 9366d0a adds src/llm/providers/openai-responses-shared.ts:359 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 9366d0a873e2; files: src/llm/providers/openai-responses-shared.ts)

PR surface

Source +2, Tests +47. Total +49 across 3 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 2 4 2 +2
Tests 1 47 0 +47
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 51 2 +49

Review metrics

Metric Value Why it matters
Production versus regression code Production +4/-2; tests +47/-0 Production growth consists of imports for the existing helper; the regression cases protect output-budget contracts at both request owners.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #127560
Summary: The canonical issue owns the missing upper cap; minimum-budget normalization and unknown-capacity preservation are distinct adjacent contracts.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Oversized output overrides reject affected provider turns, with a bounded configuration trigger and a focused repair.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (10 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-23T18:56:47.170Z sha 938ed62 :: needs real behavior proof before merge. :: none
  • reviewed 2026-08-23T19:39:00.730Z sha 938ed62 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-24T23:44:34.642Z sha 938ed62 :: needs changes before merge. :: [P2] Clamp the default simple Responses path too
  • reviewed 2026-08-25T05:25:44.853Z sha 524a728 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-02T18:12:22.937Z sha 524a728 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-22T10:57:47.100Z sha 524a728 :: needs real behavior proof before merge. :: [P1] [P1] Preserve explicit limits when model capacity is unknown
  • reviewed 2026-10-02T02:39:03.432Z sha 524a728 :: needs real behavior proof before merge. :: [P1] [P1] Preserve explicit limits when model capacity is unknown
  • reviewed 2026-10-14T00:27:19.918Z sha 6d9d058 :: needs maintainer review before merge. :: none

Reviewed October 13, 2026, 8:44 PM ET / October 14, 2026, 00:44 UTC (Revision 11).

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 23, 2026
buildOpenAIResponsesParams published an authored request maxTokens
unchanged even above the selected model's declared output capacity,
while sibling Anthropic and OpenAI Completions transports clamp
request-scoped caps (openclaw#127560). A valid agents.defaults.params.maxTokens
or per-model/per-agent/run override could make strict endpoints reject
every normal turn and compaction with a deterministic 400, and a global
override could poison fallback candidates independently.

Clamp at the canonical request boundary with the existing
clampMaxTokensToModel helper, preserving current zero/undefined
fallback semantics. The model catalog's maxTokens remains the capacity
owner. Covers both OpenAI Responses and Azure Responses, which share
this builder; Codex Responses continues stripping the field downstream.

Closes openclaw#127560

AI-assisted (Claude); clamp regression tests fail pre-fix and pass
post-fix.
@Parker-Fawcett
Parker-Fawcett force-pushed the fix/openai-responses-max-tokens-clamp branch from 1f2a167 to 938ed62 Compare August 23, 2026 18:49
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 23, 2026
applyCommonResponsesParams floored authored maxTokens at 16 but never
bounded it by the model catalog capacity, so default simple OpenAI and
Azure Responses completions (reachable from plugin LLM completion
without a managed transport requirement) could still send oversized
max_output_tokens to strict endpoints. Clamp at the shared simple-path
owner with the model cap as the authoritative upper bound while
preserving the established 16-token minimum.

Covers the ClawSweeper finding on openclaw#128282: managed and simple Responses
routes now share the same capacity boundary.
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 25, 2026
@clawsweeper clawsweeper Bot removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Sep 2, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 2, 2026
@obviyus

obviyus commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 14, 2026
@obviyus

obviyus commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus
obviyus merged commit f05fac4 into openclaw:main Oct 14, 2026
121 of 122 checks passed
@obviyus

obviyus commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Landed in f05fac4. Thanks @Parker-Fawcett! Verified with a built CLI: an over-limit maxTokens is now capped at the model's limit instead of failing the turn.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 14, 2026
…penclaw#128282)

fix(openai): cap Responses max_output_tokens at the model's output limit

The OpenAI Responses request builder forwarded a configured maxTokens even when it was above the selected model's output limit, so strict endpoints rejected otherwise valid turns with HTTP 400. The builder now caps the value at the model's maxTokens. In-range values are unchanged.

Proof: built CLI against a recording Responses endpoint that rejects budgets above a 128-token model cap, with maxTokens configured to 256. On main the request sent 256 and failed with 400; with this change it sends 128 and completes. A 64-token control is unchanged, and a live openai/gpt-5.4-mini turn completes.

Thanks @Parker-Fawcett.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 16, 2026
…128282)

fix(openai): cap Responses max_output_tokens at the model's output limit

The OpenAI Responses request builder forwarded a configured maxTokens even when it was above the selected model's output limit, so strict endpoints rejected otherwise valid turns with HTTP 400. The builder now caps the value at the model's maxTokens. In-range values are unchanged.

Proof: built CLI against a recording Responses endpoint that rejects budgets above a 128-token model cap, with maxTokens configured to 256. On main the request sent 256 and failed with 400; with this change it sends 128 and completes. A 64-token control is unchanged, and a live openai/gpt-5.4-mini turn completes.

Thanks @Parker-Fawcett.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit f05fac4)
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
Backport openclaw#128282 across both OpenAI Responses request owners.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI Responses sends max_output_tokens above the selected model capacity

2 participants