bugger.

How SGLang works: architecture & execution flow

Inference8 MIN READ

SGLang

GitHub

A language-model serving runtime with scheduling, attention kernels and reusable prefix state. Its RadixAttention idea is especially useful when conversations or related prompts repeatedly share an initial token sequence.

RadixAttentionPrefix reuseZeroMQGPU scheduling
Explore the components or trace execution
SYSTEM ARCHITECTURE SELECT A COMPONENT TO EXPLORE
Orchestration Compute State & storage┄ Feedback / repeat
Representative SGLang Python SRT text-generation path with one tokenizer manager and radix prefix caching. Boxes show responsibilities, not one process per box. Backend, model and deployment options can change cache and worker layouts.

Client / HTTP endpoint

Submits a generation request to the serving endpoint. A request can ask for streamed text, so producing and transmitting a response happens repeatedly before the request finishes.

  • Client / HTTP endpoint Tokenizer managerGeneration request
  • Tokenizer manager Client / HTTP endpointStream text to client

One pass through the system.

4 steps

Select a step to highlight the components involved in the diagram.

Why it is built this way.

Cache benefit depends on the workload

A common system prompt or repeated conversation prefix can save computation. Unrelated prompts offer less reusable state; a cache hit is not a replacement for generating the response.

Reuse needs careful memory ownership

Shared prefixes reduce duplicated state but complicate allocation and eviction. Active references must remain valid, and cached state competes with new requests for finite device memory.

Separate processes need separate diagnosis

Slow tokenization, scheduling, GPU execution and detokenization can all delay a stream. A healthy HTTP process alone does not establish that the execution pipeline is making progress.

Read the implementation.

python/sglang/srt/managers/tokenizer_manager.py

Trace request preparation and the ZeroMQ channels connecting input and output managers.

python/sglang/srt/managers/scheduler.py

Start at the scheduler and follow its batching, cache and worker collaborators.

python/sglang/srt/mem_cache/radix_cache.py

Inspect prefix matching, tree state and eviction rather than treating the cache as a text lookup.

Go deeper with the official docs

Educational diagrams, synthesized from the linked project documentation. Components and boundaries vary by version and deployment.

More AI systems under the hood.

Practice AI debugging
Inference · 8 min read

vLLM

An inference engine and serving stack for language models. Its central problem is sharing finite GPU memory and compute among requests whose prompts and response lengths are different.

Inference · 7 min read

Ray Serve

A model-serving framework built on Ray actors. It routes requests, composes deployments and scales replicas; a replica can run a PyTorch model, ordinary Python code or an LLM inference engine such as vLLM.

Training · 7 min read

PyTorch

PyTorch provides tensor operations, automatic differentiation and the building blocks for neural networks. Its central trick is to let a normal Python program describe a computation while the runtime selects hardware kernels and records how to differentiate it.

Models & tooling · 6 min read

Hugging Face Transformers

Hugging Face is an ecosystem; Transformers is its open-source model library. It connects pretrained checkpoints, model-specific implementations, preprocessing and generation APIs. Here we follow decoder-only text generation using the PyTorch backend.

Training · 7 min read

DeepSpeed

DeepSpeed wraps a PyTorch model with a training engine that coordinates precision, optimization and distributed execution. Its ZeRO optimizer removes replicated training state across data-parallel workers, enabling configurations that would otherwise exceed device memory.

Training · 8 min read

Unsloth

Unsloth adapts model loading and training operations around the PyTorch, Transformers, PEFT and TRL ecosystem. Follow a supervised LoRA or QLoRA run to see where the base model stays frozen, where gradients flow, and why specialized kernels help.

Inference · 8 min read

llama.cpp

llama.cpp provides C/C++ model inference across CPU and accelerator backends. Its core connects a model file, tokenization, a computation graph, device buffers and a sampling loop. A server can wrap this library, but HTTP is not required for local generation.