Anthropic 400 max_tokens: Field required
Why am I getting Anthropic 400 max_tokens: Field required?
A Messages API call 400s immediately, before any tokens are generated, with a body naming `max_tokens` as a missing required field — usually on the very first request against a fresh integration.
Is this your error?
{"type":"error","error":{"type":"invalid_request_error","message":"max_tokens: Field required"}}Why this happens on Anthropic
Source: https://docs.anthropic.com, verified 2026-08-08.
Anthropic's Messages API has no server-side default for max_tokens, unlike OpenAI's optional max_completion_tokens. The field exists specifically so you set a real spend ceiling — you're billed for tokens generated, not tokens reserved, so set it to your actual ceiling rather than an arbitrarily large number.
The fix
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const response = await client.messages.create({
model: MODEL,
max_tokens: 1024,
messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
});
const block = response.content[0];
console.log(block.type === 'text' ? block.text : '');What not to do
Don't retry this call unchanged — it is not a transient failure. Fix the request body first; retrying the same malformed request returns the same error every time.
Evidence
Reproduced against Anthropic's own documentation and the fixture at code-examples/javascript/anthropic/chat.ts on 2026-08-08; not yet observed in production traffic.
FAQ
Why does Anthropic require max_tokens but OpenAI does not?
Anthropic's Messages API ships no server-side default for max_tokens, so every request must set a real ceiling explicitly; OpenAI's max_completion_tokens is optional and falls back to the model's own limit.
What value should I set for max_tokens?
Your real expected output ceiling, not the model's maximum context window — you pay for tokens actually generated, so an inflated value costs nothing extra but an undersized one truncates the response.
