Cheat sheets

LLM Serving with SGLang

Start, size, measure and fix an SGLang server. SGLang 0.5.22.

  • Concepts and terms
  • Tasks and commands
  • Annotated code
  • Diagrams
  • Defaults and numbers
  • Troubleshooting
  • Questions and tips

Interactive machineSource

Start a server#

export HF_HUB_OFFLINE=1no Hub contact once the models are cached
sglang serve --model-path <model> --host 127.0.0.1 --port 30000 --enable-cache-report --enable-metrics --mem-fraction-static 0.85the L4 flags
curl http://127.0.0.1:30000/ready200 when the server accepts requests

Three processes#

HTTPZMQbatchslotsidstextclientClientPOST /generateprocessTokenizerManagertext to token idsprocessSchedulerbatches and KV poolprocessDetokenizerids to textmodelModel on GPUforward passstoreKV poolslots per tokenHTTPZMQbatchslotsidstextclientClientPOST/generateprocessTokenizerManagertext totoken idsprocessSchedulerbatches andKV poolprocessDetokenizerids to textmodelModel onGPUforwardpassstoreKV poolslots pertoken

Requests and checks#

TaskCode
Send one native requestPOST /generate
Ask in the OpenAI chat formatPOST /v1/chat/completions
Read the cache hits of one requestmeta_info field cached_tokens
Probe readiness/ready
Fix the length spread of a benchmark--random-range-ratio 1.0
Sweep one concurrency cap--max-concurrency 8
Send a fixed arrival rate--request-rate <rate>
Test output stabilitypython3 -m sglang.test.test_deterministic --n-trials 50 --test-mode single

Benchmark your server#

CommandWhat it does
python -m sglang.benchmark.serving --backend sglang --port 30000 --dataset-name random --num-prompts 32 --max-concurrency 8 --seed 7Sends 32 random prompts with at most 8 in flight, then prints the report
--random-input-len 512 --random-output-len 128Sets the prompt and answer lengths
--random-range-ratio 1.0Keeps every prompt at the length you asked for. The default 0.0 makes the average about half
--request-rate <rate>Sends requests on a schedule, so the server can fall behind like real traffic

Defaults that matter#

SettingDefaultNote
--schedule-policyfcfslpm is the paper's order, not the default
--schedule-conservativeness1.0Raise it when the log shows retractions
--radix-eviction-policylruChange only after a measured hit rate
--kv-cache-dtypeautoKeeps the model data type
--stream-interval1Streams after every step
--tp-size1One GPU unless the weights need more
--cache-threshold0.3Gateway match rate for prefix affinity

Numbers to watch#

sglang:token_usage
near 1.0 means the KV pool is full.
sglang:num_queue_reqs
above 0 for minutes means the server is past its knee.
sglang:cache_hit_rate
a gauge over one interval. Compute the rate from sglang:prefill_effective_tokens_total.
cached_tokens
the count of prompt tokens reused from the cache.
Goodput
requests per second that meet the TTFT and TPOT targets together.

Incidents on record#

Issue #36889
a Mamba cap set the request limit to 2, and only an info line said so.
Issue #17411
a FlashInfer upgrade made Hopper decode seven times slower.
Issue #5274
EAGLE-3 doubled batch-1 speed and cut concurrency 20 by a quarter.
Issue #35129
removing a per-session hash raised the hit rate from 66.8 to 94.4 percent.

Tips#

  • Start servers with --enable-cache-report and --enable-metrics.
  • Read the pool size in the startup log before you size clients.
  • Send SIGTERM to the main process only.
  • Set SGLANG_ENABLE_HEALTH_ENDPOINT_GENERATION=false for liveness probes.

Fix these errors#

SymptomCauseFix
RuntimeError: [METAL] Command buffer execution failed at 32 requestsPool sized at 87 percent of free memoryPass --mem-fraction-static 0.4 and restart
Median TTFT near 28 s with free GPU memoryAdmission queues requests for a full poolRaise --schedule-conservativeness or lower --max-running-requests
KV cache pool is full. Retract requests. in a loopThe forecast is too boldRaise --schedule-conservativeness and send realistic max_new_tokens
RuntimeError: The memory capacity is unbalancedOne TP card shares memory with other podsFree the other card and restart. No flag disables the check
cuda graph: False on decode lines under loadDecode batch above the graph limitSet --cuda-graph-max-bs-decode to peak concurrency
ambiguous option: --cuda-graph-max-bsThe flag was split at v0.5.22Use --cuda-graph-max-bs-decode or --cuda-graph-max-bs-prefill
#cached-token: 0 on every repeatRandom ID at the start of the system promptMove the ID to the end or remove it

Tune in this order#

1Measurerun sglang.benchmark.serving at three caps with--random-range-ratio 1.0.2Memoryset --mem-fraction-static on any shared machine.3Promptsput shared text first and keep request IDs outof the start.4Capset --max-running-requests at the knee youmeasured.5Kernelschange graph and attention settings one at atime.6Parallelismadd --tp-size or --dp-size only when one replicasaturates.

Glossary#

ITL
inter-token latency, the gap between tokens.
MLA
multi-head latent attention, a compact KV layout used by DeepSeek.
MoE
mixture of experts, where each token uses a few experts.
EPLB
expert placement load balancing across GPUs.
PD
prefill-decode disaggregation, with prefill and decode on separate servers.

Questions#

Where do I look when time to first token is high and the GPU is idle?
Check the tokenizer process before the kernels.
Why does a bigger pool add no throughput?
The pool was never full at 64 users in our runs.
Should I deploy latest?
No. Pin a tag such as v0.5.22.