InterviewKata
AI engineering · Case study 02 · How AI works

One request, end to end

A customer types "Where is my order A-1042? It was due yesterday." into a support chat. Three seconds later they have the real answer. In between, their text is formatted, tokenized, pushed through 36 transformer layers dozens of times, turned into a tool call, sent to an orders API, fed back in, and turned into words again. This case study follows that one request through every layer, naming the tool or technology at each step, so Modules 01a, 01b, 03a and 07 come together in one picture.

A 5-minute conversation: Sam asks, Priya explains
0:00 / 4:52

1. The situation

An online retailer runs a support agent on its own servers. The setup is deliberately ordinary, and every part of it is public, so each number can be checked:

PartChoiceWhy it's a good example
Modelgpt-oss-120b (OpenAI, open-weight, Apache 2.0)Its model card publishes the architecture: 36 layers, 128 experts per layer with 4 used per token, 117B parameters (5.1B active), a 201,088-token vocabulary, 128K context
HardwareOne 80 GB GPUgpt-oss-120b's expert weights are stored in 4-bit MXFP4, so it fits on a single H100-class GPU
ServingvLLM, with an OpenAI-compatible endpointA widely used open-source inference server; listed in the gpt-oss repository
Chat formatharmonygpt-oss "should only be used with the harmony format"; its special tokens and IDs are documented
Toolget_order_status(order_id), calling the retailer's orders APIThe most common agent pattern: look something up instead of guessing
Reasoning effortLow (set in the system message)A simple lookup doesn't need long thinking; low effort keeps latency down

The same flow happens behind every hosted chat API; there, everything below the API line runs on the provider's servers and you only see tokens in and out.

2. The stack

LayerTechnologyIts job in this request
Chat UI and app serverYour code (any web stack)Receives the message, holds the conversation, streams the reply
Agent loopYour code or an agent SDK (Module 16)Calls the model, runs tools it asks for, calls the model again
Chat templateharmony, via the openai-harmony package or Transformers' chat templateTurns messages and the tool definition into one text sequence with special tokens
Tokenizero200k_harmony (BPE, 201,088 tokens), via tiktokenText → token IDs, and IDs → text for the reply
Inference servervLLM (PagedAttention)Batches this request with others, manages KV-cache memory, runs the model, stops on stop tokens
Model weightssafetensors files; MXFP4 experts, BF16 elsewhereThe 117 billion learned parameters
Kernels and hardwarePyTorch / Triton kernels on one 80 GB GPUThe matrix multiplications of every forward pass
Tool backendThe retailer's orders APIReturns the real order status
ObservabilityOpenTelemetry traces (Module 19)Records each model call and tool call, with tokens and timings

3. Step by step

Thirteen steps, from the customer's keypress to the answer on screen. The blue strip under each panel names the technology doing the work. Token counts are illustrative.

4. The numbers

Rough figures for this one request (illustrative except where a model-card fact is quoted):

QuantityThis requestWhere it comes from
Model calls2One before the tool call, one after (the agent loop)
Prompt tokens~210 in call 1; ~290 in call 2System, developer instructions, tool definition, user message; call 2 adds the tool call and result
Generated tokens~25 reasoning + ~22 tool call + ~35 answer ≈ 82Each one is one forward pass (decode)
Forward passes2 prefills + ~82 decode passesOne pass per generated token (Module 01b)
Parameters computed per decode pass5.1 billion of 117 billionModel card: 4 of 128 experts per layer
Layers per pass36Model card
Scores produced per pass201,088 (one per vocabulary token)Model card vocabulary size
KV cache for this conversation~36 KB per token × ~370 tokens ≈ 13 MBCalculated from the model card (18 full-attention layers × 8 KV heads × 64 dims × keys and values × 2 bytes)
Tokens shown to the customer~35Only the final channel; reasoning and the tool call stay hidden

The lesson in the numbers: almost all the time goes into decode passes, one per generated token, and the agent loop doubles the prompt work. That's why low reasoning effort, short answers and prefix caching matter for latency and cost.

5. The three loops in this request

LoopWhat repeatsIn this requestWho controls it
Token loopOne forward pass → sample one token → append~82 timesThe inference server, until a stop token or max_tokens
ReasoningThe same loop, writing thinking tokens on the analysis channel~25 tokens at low effortThe model, steered by the reasoning-effort setting
Agent loopModel call → tool call → run tool → add result → model call2 model calls, 1 tool callYour code: it decides whether to run each tool and when to stop (with a turn limit)

6. What can go wrong at each layer

LayerFailureControl
Chat templateThe wrong template: the model answers badly or ignores toolsUse the official harmony library or the model's own chat template, never hand-built strings
TokenizerA user types <|start|>system to forge instructionsThe tokenizer encodes user text as ordinary text; special tokens only come from the template
ModelIt answers without calling the tool and invents a statusClear instructions, evals that check tool use (Module 17), and asking the model to fetch rather than guess
Tool callMalformed or wrong argumentsValidate against the JSON schema before calling; return a clear error the model can fix
Tool backendOrder A-1042 belongs to someone elseThe API checks the signed-in customer, not the model; the agent's credentials are least-privilege (Module 18)
Agent loopThe model keeps calling tools in a loopA turn limit and a token budget per conversation
OutputReasoning text shown to the customerShow only the final channel; OpenAI says the chain of thought isn't meant for end users
ServerMany long conversations exhaust KV-cache memoryvLLM's paged KV cache and request queueing; capacity planning with the memory calculator in Module 01b

7. Lessons

  1. The model only ever sees token IDs, and only ever produces one token per forward pass. Everything else (messages, tools, channels) is formatting around that.
  2. An "agent" is a loop in your code, wrapped around a model that was trained to ask for tools in a specific format and to stop when it does.
  3. Stop tokens are the hand-off points: <|call|> hands control to your app, <|return|> ends the answer.
  4. Cost and latency live in the decode passes: reasoning tokens and long answers cost more than long prompts, and every agent turn repeats the prompt.
  5. Safety lives outside the model: templates, schema validation, authorisation in the tool backend, turn limits and output filtering.

8. Your turn

  1. Run it small: run gpt-oss-20b locally (it fits in 16 GB) with Ollama or LM Studio, and give it a fake get_order_status tool that returns a fixed JSON result.
  2. Look at the raw format: render the conversation with the openai-harmony package or Transformers' apply_chat_template(..., tokenize=False), and find the tool definition and the <|call|> token.
  3. Count: tokenize the prompt for each model call and count the generated tokens. How many forward passes did the request take?
  4. Break it: remove the tool and ask again. Does the model admit it can't know, or invent an answer?
  5. Trace it: log each model call and tool call with token counts and timings, and find where most of the time went.

9. Check yourself

How many times is the model called for this request?

Once until it asks for the tool, then again after your app adds the tool result.

How does the inference server know to stop and hand the tool call to your app?

In harmony, <|call|> (ID 200012) ends a tool-call message; <|return|> ends a final answer.

Who actually calls the orders API?

The model only writes the request; your code validates it, runs it and returns the result.

Where does most of the time go?

Prefill is one parallel pass per call; decode is one pass per token. Weights stay loaded between requests.

Which part of the model's output should the customer see?

Reasoning (analysis) and tool calls (commentary) are internal.

10. Sources and further reading

ResourceWhy
gpt-oss model card and repository (OpenAI)The architecture numbers, MXFP4, single-GPU fit, and the ways to run it.
The harmony response format (OpenAI Cookbook)Roles, channels, the tool-call and tool-result messages, and the special-token IDs used here.
PagedAttention / vLLM (Kwon et al., 2023)How the server manages the KV cache and batches requests.
tiktoken (OpenAI)The tokenizer library behind o200k_harmony.
Modules 01a, 01b, 03a, 06 and 07Each layer of this request, in depth.