Glossary

Max tokens

A request parameter that caps how many tokens a language model may generate in one response; when it is reached, output stops, even mid-sentence.

Max tokens is the request parameter that caps how many tokens a language model may generate in one response. When the model reaches the limit it stops, even mid-sentence, and the API reports that the response was cut off. It limits output only; the input you send is governed by the model’s context window. Setting it well protects against runaway cost and latency, while setting it too low produces truncated answers, broken JSON and, with reasoning models, responses that end before any visible answer appears. It does not make a model more concise on its own, because the model does not plan its answer around the limit.

What max tokens controls

Every request has two token budgets. Input tokens are everything the model reads, and output tokens are everything it writes. Max tokens sets the ceiling on the second. Generation ends at whichever comes first: the model finishing naturally, a stop sequence appearing, or the output reaching the max tokens value.

Providers name the parameter differently. Common forms are max_tokens, max_output_tokens and max_completion_tokens, and some APIs require it on every request while others apply a default. Each model also has its own hard ceiling on output, which can be much smaller than its context window. A model with a window of several hundred thousand tokens may still cap a single response at a few thousand or a few tens of thousands of tokens, and asking for more than that is rejected or silently reduced.

Input and output must also fit in the window together. If a model has a 128,000-token window and the request carries 120,000 tokens of input, there are at most 8,000 tokens left for output, whatever max tokens says. Long prompts therefore squeeze the room for answers, which is one more reason to keep input lean.

How truncation shows up

When generation stops because of the limit, the response includes a finish or stop reason indicating length rather than a natural end. The text simply stops. For prose, that means a sentence cut in half. For structured output, it means invalid JSON: a missing closing brace or a string left open, which breaks the parser downstream. For function calling, it means incomplete arguments. Applications should always check the stop reason and treat a length stop as an error or a signal to continue, never as a complete answer.

Reasoning models add a twist. Their internal reasoning tokens are generated before the visible answer and usually count against the same output limit. If the limit is tight, the model can spend it all on reasoning and return an empty or partial answer. Providers that offer reasoning models generally advise leaving generous headroom, or controlling reasoning separately through an effort or budget setting, a topic covered under test-time compute.

Why set it at all

Cost control. Output tokens are usually priced several times higher than input tokens. A capped response has a capped cost, which matters when a prompt occasionally triggers a very long answer, or when a bug causes a model to repeat itself until it is stopped.

Latency. Output is generated one token at a time, so response time grows with output length. A limit bounds the worst case, which helps interactive features where a slow answer is a failed answer. LLM latency and streaming responses cover the other levers.

Capacity and rate limits. Some providers reserve capacity, or count requests against tokens-per-minute quotas, using the requested max tokens rather than the number of tokens generated. Setting it far higher than needed can then lower throughput or trigger rate limiting sooner than expected.

Predictable formats. For tasks with a known output size, such as a classification label or a short summary, a tight limit catches prompts that drift into long explanations.

Choosing a value

The right value is the longest legitimate answer for the task, plus headroom. Estimate it from real outputs: run a representative sample, record output lengths, and set the limit comfortably above the longest. A rough guide by task type looks like this.

Task

Typical output

Sensible starting limit

Classification or routing

A label or short JSON object

50 to 200 tokens

Short answer or chat reply

A few sentences to a few paragraphs

500 to 1,500 tokens

Structured extraction

JSON with many fields or line items

2,000 to 8,000 tokens, sized to the largest document

Long-form drafting

Several pages of text

4,000 tokens or more, within the model’s output cap

Reasoning model, any task

Reasoning plus answer

Several times the expected answer length

These are starting points, not rules. Token counts depend on tokenization, so the same text can cost more tokens in one model than another, and non-English text often uses more tokens per word. Revisit the value when you change models, since output caps and tokenizers differ, and whenever prompts or schemas change.

Getting length right without relying on the limit

Because a model does not see the limit as a target, the way to get shorter answers is to ask for them. State the length or format in the prompt: three bullet points, a single sentence, a JSON object with these fields. Constrain the shape with a schema through JSON mode or constrained decoding, which also makes outputs smaller by removing filler. Few-shot prompting with short examples sets the expected length by demonstration. Treat max tokens as the safety net beneath these, not the main control.

Handling long outputs

Some tasks need more output than one response allows: extracting hundreds of line items, translating a long document, or writing a full report. There are three common approaches. The first is continuation: when a response stops on length, send a follow-up asking the model to continue from where it stopped, then join the parts. It works for prose and is fragile for JSON. The second is chunking the task: split the input into sections and process each separately, so each output fits comfortably. The third is changing the output design: return identifiers or short records instead of verbose text, and assemble the final result in code.

A worked example

An invoice-processing pipeline asks a model to return every line item as JSON with max tokens set to 1,024, a value copied from an earlier chat feature. Most invoices have 10 to 30 lines and fit. A supplier sends a 140-line invoice; the output reaches 1,024 tokens at line 61, stops mid-object, and the JSON parser throws. Because the pipeline does not check the stop reason, the error is logged as a parsing bug and the invoice is skipped.

The fix has three parts. The pipeline checks for a length stop and retries large documents in sections of 40 lines. The limit is raised to 6,000 tokens, sized from the largest invoices in a sample of 500. And the schema is tightened so each line item carries five short fields instead of free-text descriptions, which cuts output per line by about a third. The figures are illustrative, but the pattern is common in invoice processing and other document extraction work.

Max tokens in agents

Agents call a model many times per task, and each call has a different job. A planning step may need a few hundred tokens, a tool call needs only its arguments, and a final report may need thousands. One global limit for all of them either leaves small steps unguarded or truncates large ones. Setting the limit per step type, and logging how often each type stops on length, shows clearly when a step needs more room. A length stop inside an agent loop is especially costly: the truncated tool call fails, the agent retries with the same context, and the whole input is billed again. Tracking length stops as a metric alongside errors and latency, through agent observability, catches these loops early, before a small misconfiguration becomes a large bill.

Max tokens and the rest of the context budget

Max tokens is one line in a wider budget. The context window has to hold the system prompt, tool definitions, history, retrieved passages, the user message and the response. As agents run longer, input grows and the room left for output shrinks, which can force smaller limits or cause requests to fail outright. Planning that budget explicitly, as described in a context budget template for AI agents, avoids surprises. Context compression and context pruning free space on the input side so the response has room.

Where Exabase fits

Exabase does not set or enforce max tokens; that stays in your model calls. It helps on both sides of the budget. On the input side, Exabase Memory replaces resent conversation history with a few hundred tokens of relevant facts, and Deep Search returns specific passages instead of broad chunks, so more of the window is left for the answer. The reduce token costs solution shows an illustrative request falling from about 15,100 input tokens to about 2,800.

For extraction tasks that would otherwise push a model to its output limit, Exabase Extract does the parsing before any model is involved. Submitting a file to POST /v2/extract returns text chunks with page numbers or timestamps, and invoices, resumes and contracts are recognised automatically, with structured fields such as an invoice’s vendor, line items and total. Chunks are fetched in pages, so a 200-page document never has to pass through a single model response, and adding ?format=markdown returns a compact Markdown version ready for a prompt. The retrieving results guide covers polling, webhooks and chunk paging, and the file extraction solution describes the wider pattern.

Frequently asked questions

What happens when a model hits max tokens?

It stops generating immediately, often mid-sentence, and the API reports a length-based stop reason. The partial output is returned, so applications should check the stop reason before treating the answer as complete.

Does max tokens include the input?

No. It limits output only. Input and output together must still fit in the context window, so a very long prompt can leave less room for the answer than max tokens allows.

Does a lower max tokens value make answers shorter?

Only by cutting them off. Models do not plan their answers around the limit, so ask for the length or format you want in the prompt and use max tokens as a safety cap.

Do reasoning tokens count towards max tokens?

Usually yes. Reasoning models generate internal reasoning before the visible answer, and it typically counts against the output limit, so a tight limit can leave no room for the answer.

What should I set max tokens to?

The longest legitimate output for the task plus headroom, measured from a sample of real responses. Classification needs very little; structured extraction and reasoning models need much more.

How can Exabase help with output limits?

Exabase Extract parses documents into paged chunks and structured fields before a model sees them, so large documents do not have to fit in one response, and Memory and Deep Search shrink input to leave more of the window for output.

Related pages: input vs output tokens, token, context window, structured output, reasoning models, streaming responses, temperature, file extraction, best document extraction APIs

Cut your token spend and give your agent precise context.

Get started in minutes.

Cut your token spend and give your agent precise context.

Get started in minutes.