OpenAI API - Max Tokens
The word-count leash for chat completions
- other_options
- Options
Language models will happily talk forever, and when you're running one inside a ComfyUI workflow that's usually a bug, not a feature. OAIAPI_MaxTokens sets a hard ceiling on how many tokens a chat completion is allowed to generate - the author's tooltip calls it "an upper bound for the number of tokens that can be generated for a response," and that's exactly what it is. For prompt rewriting or image classification you rarely need more than a couple of hundred tokens of output, so capping it saves you money on cloud APIs and saves you time on local servers.
How it works
Same architecture as every option node in the pack: it packs max_tokens into an OAIAPI_OPTIONS payload, and the Chat Completion node pulls it out and passes it as the named max_tokens argument. The slider runs 1 to 1,000,000 (default 512). That upper bound looks absurd, but there's a method to it - the author set the max that high on purpose because ComfyUI would otherwise apply its own default cap of 2048, which is too low for models that need room to think.
The inputs and output
- max_tokens - the only widget. 512 is a solid default for "rewrite this prompt" or "answer in one sentence." If you're doing long-form text generation, raise it; if you're parsing a structured reply, you can often drop it to 128 and speed things up.
- other_options (optional) - for chaining option nodes.
- Output: Options (
OAIAPI_OPTIONS), feeding the next option node or Chat Completion'soptionsinput.
Installing it
Part of the same pack, so same install: ComfyUI Manager → "OpenAI API" → install, or
cd ComfyUI/custom_nodes
git clone https://github.com/hekmon/comfyui-openai-api
restart, and you're done. Only new pip dependency is openai; no model downloads.
Where people get burned
The subtle one is reasoning models. OpenAI's o-series and similar "thinking" models count their hidden reasoning tokens against the same budget, so if you cap max_tokens tight on an o1 model you'll get back a wall of thinking and no actual answer. Give reasoning models a generous budget or switch them to a non-reasoning model for short tasks. There's also a behind-the-scenes detail worth knowing: the source deliberately sends max_tokens rather than the newer max_completion_tokens field because vLLM has implemented the new one but Ollama and TGI haven't - so this node stays compatible with all the local servers people actually run, at the cost of being technically "legacy" against the cloud API. It just works, which is the point.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| max_tokens | INT | 5121–1000000 | An upper bound for the number of tokens that can be generated for a response |
| other_optionsopt | OAIAPI_OPTIONS | Others options to merge with |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| Options | OAIAPI_OPTIONS | Merged options to forward |