New Tool Radar

AI Concepts

How to Set vLLM’s Maximum Context Length With –max-model-len

Start vLLM with --max-model-len to cap the context window, trading long-document support for lower memory use.

Schematic diagram with four steps: start vLLM with --max-model-len 8192, confirm a short chat works, confirm an over-long request is refused, then check GPU memory is lower.

vLLM sizes its KV cache from the model’s maximum context length, so a model that supports 128k tokens reserves far more GPU memory than the same model capped at 8k. If your prompts are short, cap the length with --max-model-len and the server starts faster and lighter.

Start the server with a context cap

Add the flag when you start the server: vllm serve Qwen/Qwen2.5-1.5B-Instruct --max-model-len 8192. Requests whose prompt plus requested output would exceed 8192 tokens are rejected, so pick a cap that fits your longest real prompt. The cap also bounds the KV cache, which is what makes the server lighter.

vllm serve Qwen/Qwen2.5-1.5B-Instruct --max-model-len 8192
# a request past the cap is rejected; a short one works:
curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "Qwen/Qwen2.5-1.5B-Instruct", "messages": [{"role": "user", "content": "Hello!"}]}'

Do not set the cap below what your documents need: a capped server cannot be talked into a longer context by a client flag. If you serve a chat model, the chat template still applies, and the template tokens count toward the same cap.

Try it: four checks

Check What you should see If you do not see it
Server starts vllm serve starts without a length error Check the cap is not larger than the model supports
Short chat works A short chat completion returns a reply Check the model name matches the served model
Long request rejected An over-cap request is refused with an error That is the cap working, not a bug
Memory lower The server uses less GPU memory than uncapped Compare with the uncapped start

Expected result. We checked the vLLM Quickstart and the OpenAI-Compatible Server docs, which give the vllm serve command and the /v1/chat/completions shape. We did not run vLLM on this machine.

Limits

This guide covers the context cap only. The exact flag name and behaviour can change between vLLM versions, so check vllm serve --help on your install. We did not test the flag ourselves.

Sources

See all