Repository navigation
fix: guard Anthropic Messages max tokens - #66664
Conversation
🔒 Aisle Security AnalysisWe found 2 potential security issue(s) in this PR:
1. 🟡 Unbounded `max_tokens` forwarded to Anthropic Messages API allows resource/cost exhaustion
Description
This allows a caller to set extremely large values (e.g.,
Vulnerable code: const requested = resolvePositiveAnthropicMaxTokens(params.requestedMaxTokens);
if (requested !== undefined) {
return requested; // no clamp
}
...
max_tokens: maxTokens,RecommendationClamp runtime overrides to a safe upper bound (model and provider caps) before sending to Anthropic. For example: function resolveAnthropicMessagesMaxTokens(params: {
modelMaxTokens: number | undefined;
requestedMaxTokens: number | undefined;
}): number | undefined {
const requested = resolvePositiveAnthropicMaxTokens(params.requestedMaxTokens);
const modelMax = resolvePositiveAnthropicMaxTokens(params.modelMaxTokens);
const providerCap = 32_000;
const effectiveMax = modelMax !== undefined ? Math.min(modelMax, providerCap) : providerCap;
if (requested !== undefined) {
return Math.min(requested, effectiveMax);
}
return modelMax !== undefined ? effectiveMax : undefined;
}Additionally, if 2. 🔵 User-visible error leaks internal model provider/id in Anthropic transport
Description
If the transport stream is consumed by end users (e.g., CLI/UI/API clients), this can disclose internal model identifiers/provider routing decisions.
Vulnerable code (throw includes internal identifiers): throw new Error(
`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`,
);Propagation to user-visible stream error: output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
stream.push({ type: "error", reason: output.stopReason as never, error: output as never });RecommendationAvoid including internal model/provider identifiers in errors that can be returned to end users. Options:
if (maxTokens === undefined) {
// log/debug: { provider: model.provider, modelId: model.id }
throw new Error("Anthropic Messages transport requires a positive maxTokens value");
}
const safeMessage = error instanceof Error
? "Request failed due to invalid configuration"
: "Request failed";
output.errorMessage = safeMessage;Use a structured internal error (with cause/details) for observability, while returning a stable, non-sensitive message to clients. Analyzed PR: #66664 at commit Last updated on: 2026-04-14T21:59:08Z |
Greptile SummaryThis PR fixes the Anthropic Messages transport to reject non-positive Confidence Score: 5/5
Reviews (1): Last reviewed commit: "openclaw-e82: guard Anthropic Messages m..." | Re-trigger Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e4e7012f90
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| function resolvePositiveAnthropicMaxTokens(value: unknown): number | undefined { | ||
| return typeof value === "number" && Number.isFinite(value) && value > 0 | ||
| ? Math.floor(value) |
There was a problem hiding this comment.
Validate floored maxTokens remains positive
resolvePositiveAnthropicMaxTokens checks value > 0 before flooring, so fractional inputs in (0, 1) (for example 0.5 from extra params) become 0 and are treated as valid downstream. That lets resolveAnthropicMessagesMaxTokens return 0, bypasses the undefined guard, and still sends max_tokens: 0 to Anthropic, which reintroduces the provider-side failure this change is trying to prevent.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 544a76e. The helper now floors first and only accepts the result if it remains positive, and I added a regression covering a fractional override (maxTokens: 0.5) so it falls back to the model limit instead of sending max_tokens: 0.
Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix.
Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix.
e4e7012 to
544a76e
Compare
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
* Docs: add Anthropic max_tokens investigation memo Regeneration-Prompt: | Investigate the reported OpenClaw cron isolated-agent failure where an Anthropic Haiku run returned "max_tokens: must be greater than or equal to 1". Do not implement a fix yet. Inspect the cron isolated-agent execution path, the embedded runner, extra param plumbing, Anthropic transport code, and any model-selection or token-budget logic that could synthesize maxTokens = 0. Produce a concise maintainer memo with concrete file references, explain why cron itself is not the component setting maxTokens, identify the most likely root cause, describe the smallest repro shape, and recommend the cleanest fix. * openclaw-e82: guard Anthropic Messages maxTokens Regeneration-Prompt: | Fix the Anthropic Messages path so OpenClaw never sends max_tokens <= 0 to Anthropic. Match the positive-number guard already used by the Anthropic Vertex transport, but keep the change scoped: validate token limits in src/agents/anthropic-transport-stream.ts where transport options are resolved and where the final payload is assembled, fall back to the model limit when a runtime override is zero, fail locally when no positive token budget exists, and drop non-positive maxTokens from src/agents/pi-embedded-runner/extra-params.ts so hidden config params do not leak through. Add focused regression coverage for both the transport and extra-param forwarding path, and remove the earlier investigation memo from the branch so the PR diff only contains the fix. * fix: scope Anthropic max token guard * fix: document Anthropic max token guard * fix: floor Anthropic max token overrides
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
Port from openclaw/openclaw#66664. The build_anthropic_kwargs call site used 'max_tokens or _get_anthropic_max_output(model)', which correctly falls back when max_tokens is 0 or None (falsy) but lets negative ints (-1, -500), fractional floats (0.5, 8192.7), NaN, and infinity leak through to the Anthropic API. Anthropic rejects these with HTTP 400 ('max_tokens: must be greater than or equal to 1'), turning a local config error into a surprise mid-conversation failure. Add two resolver helpers matching OpenClaw's: _resolve_positive_anthropic_max_tokens — returns int(value) only if value is a finite positive number; excludes bools, strings, NaN, infinity, sub-one positives (floor to 0). _resolve_anthropic_messages_max_tokens — prefers a positive requested value, else falls back to the model's output ceiling; raises ValueError only if no positive budget can be resolved. The context-window clamp at the call site (max_tokens > context_length) is preserved unchanged — it handles oversized values; the new resolver handles non-positive values. These concerns are now cleanly separated. Tests: 17 new cases covering positive/zero/negative ints, fractional floats (both >1 and <1), NaN, infinity, booleans, strings, None, and integration via build_anthropic_kwargs. Refs: openclaw/openclaw#66664
What
This PR hardens the Anthropic Messages transport so OpenClaw never forwards non-positive
max_tokensvalues to Anthropic, and it drops invalidmaxTokensextra params before they can leak into stream options.Why
A cron-isolated agent turn failed with Anthropic's
max_tokens: must be greater than or equal to 1error. The runtime needed a positive-number guard in the Anthropic Messages path, matching the existing Anthropic Vertex behavior, so invalid token limits fail locally instead of reaching the provider API.Changes
Testing
pnpm test src/agents/anthropic-transport-stream.test.ts src/agents/pi-embedded-runner-extraparams.test.ts