LLM application anatomy and APIs
A production LLM feature is mostly ordinary software wrapped around a few calls to a model API. Those calls are remote, slow, metered, rate-limited and probabilistic, and every one of those properties creates defects that have nothing to do with the quality of the model. This chapter takes the running example, the Stride support assistant, apart: its components, the request and response of a model call, the ways a call ends, streaming, errors and retries, cost, and what must be recorded so that any behaviour can be reproduced and tested.
Prerequisites: How LLMs generate text; basic HTTP and JSON (API & Integration).
Architecture of an LLM application
The Stride assistant answers questions about policies and orders. Its request path looks like this:
browser chat widget
│ HTTPS, user session
▼
API gateway ─────────── authentication, rate limiting, request size limits
│
▼
conversation orchestrator ── loads conversation state, enforces turn and cost budgets
│
├─► input guardrails ─────── blocks abusive or out-of-scope input, redacts PII
│
├─► context assembly ─────── system prompt template (versioned), history window,
│ retrieved policy chunks, user profile, tool definitions
│
├─► model client ─────────── provider SDK, timeouts, retries, fallback model
│ │
│ └─► tool loop ────── get_order_status(order_id) with authorization check
│
├─► output parsing ───────── structured output validation, citation check
│
├─► output guardrails ────── policy and safety filters
│
└─► response + trace ─────── answer, citations, trace ID, usage and latency metricsOnly the model calls inside the model client are probabilistic. Everything else is deterministic code and should be tested with ordinary unit and integration tests, using a fake model where a model call is involved (Testing LLM code without the model).
| Component | Deterministic? | Typical defects | Main test technique |
|---|---|---|---|
| API gateway | Yes | Missing auth, no size limits, no rate limits | API and security tests |
| Orchestrator | Yes | Lost or mixed-up conversation state, unbounded loops, no budgets | Unit and integration tests |
| Input guardrails | Mostly (may call a classifier model) | Over-blocking, under-blocking, PII leaking into prompts | Classification metrics on labelled inputs |
| Context assembly | Yes | Wrong template version, history window drops the system prompt, wrong user's data | Unit tests and snapshot tests of rendered prompts |
| Model client | Wrapper yes, model no | Wrong stop-reason handling, retry storms, missing timeouts | Fake-client tests, fault injection |
| Tool loop | Wrapper yes | Missing authorization, unvalidated arguments, infinite loops | Unit tests, agent evaluation |
| Output parsing | Yes | Crashes on invalid JSON, accepts invalid citations | Unit tests with recorded bad outputs |
| Output guardrails | Mostly | Same as input guardrails | Classification metrics |
| Model output quality | No | Wrong, unfaithful, irrelevant or unsafe answers | Evaluation with datasets and scorers |
The table shows a recurring point: most of an LLM application is testable with conventional techniques. Model evaluation is necessary but it is not a substitute for those tests.
The model call: request and response
All major chat APIs share one conceptual shape: a list of messages with roles, plus parameters, sent to a model; the response contains generated content, a stop reason and usage counts.
{
"model": "<pinned model id>",
"messages": [
{"role": "system", "content": "You are Stride's support assistant. Answer only from the POLICY section..."},
{"role": "user", "content": "Can I return shoes I wore once?"}
],
"temperature": 0,
"max_tokens": 500,
"tools": [{"name": "get_order_status", "description": "...", "parameters": {"type": "object"}}]
}| Role | Purpose | Testing note |
|---|---|---|
| system (or developer) | Instructions from the application: persona, rules, format | Must survive history truncation; treat as public, never put secrets in it |
| user | The end user's message | Untrusted input |
| assistant | Earlier model replies, or a prefilled start of the reply | History you send back is also input; it can be manipulated if stored client-side |
| tool | Result of a tool call | Untrusted data from an external system |
The same call in three common SDKs (run on your own machine; set the API key and a pinned model ID in environment variables):
# OpenAI Chat Completions API
import os
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model=os.environ["LLM_MODEL"],
messages=[
{"role": "system", "content": "You are Stride's support assistant."},
{"role": "user", "content": "Can I return shoes I wore once?"},
],
temperature=0,
max_completion_tokens=500,
)
choice = response.choices[0]
print(choice.message.content)
print(choice.finish_reason, response.usage.prompt_tokens, response.usage.completion_tokens)# OpenAI Responses API
import os
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model=os.environ["LLM_MODEL"],
instructions="You are Stride's support assistant.",
input="Can I return shoes I wore once?",
max_output_tokens=500,
)
print(response.output_text)
print(response.status, response.usage.input_tokens, response.usage.output_tokens)# Anthropic Messages API: the system prompt is a top-level parameter and max_tokens is required
import os
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model=os.environ["LLM_MODEL"],
system="You are Stride's support assistant.",
messages=[{"role": "user", "content": "Can I return shoes I wore once?"}],
temperature=0,
max_tokens=500,
)
print(message.content[0].text)
print(message.stop_reason, message.usage.input_tokens, message.usage.output_tokens)Differences between providers are small but they matter for tests: where the system prompt goes, whether max_tokens is required, how content is structured (a string or a list of content blocks), and the names of stop reasons and usage fields. Many teams wrap providers behind one internal interface, or use a gateway library such as LiteLLM, so that the rest of the application and its tests deal with one normalized shape.
Stop reasons: how a call ends
Every response says why generation stopped. Each reason requires different handling, and each handling path needs a test.
| Normalized meaning | OpenAI Chat finish_reason | Anthropic stop_reason | Correct application behaviour |
|---|---|---|---|
| Finished normally | stop | end_turn, stop_sequence | Validate and deliver |
| Hit the output limit | length | max_tokens, model_context_window_exceeded | Never deliver as complete: retry with a larger limit, or shorten the request, or show a notice |
| Wants to call a tool | tool_calls | tool_use | Execute the tool (with authorization) and call the model again |
| Blocked or refused | content_filter | refusal | Show a safe template and log for review |
| Must continue | not applicable | pause_turn (server-side tool loops) | Send the conversation back to continue |
The most common production bug in this area is treating a truncated response as complete. It shows up as cut-off sentences in the UI, or as JSON parse errors that are blamed on the model. The handler below maps provider values to one vocabulary and decides the next action; the test cases cover every branch, including an unknown value from a future API version, which must fail closed.
Run the code to see the result.
Output:
normal answer -> deliver answer
tool request -> run 1 tool call(s) and call the model again
truncated, small budget -> retry once with a larger max_tokens
truncated, large budget -> show a shortened-answer notice and log a truncation
provider refusal -> show the refusal template and log for review
new provider value -> fail closed: unknown stop reasonStreaming
With streaming, the API sends the response as a sequence of events over one HTTP connection (server-sent events): a start event, many small text deltas, possibly tool-call deltas, and a final event with the stop reason and usage. Users see text appear immediately, which is why chat interfaces stream.
Streaming creates its own failure modes:
- Interrupted streams. The connection can drop after some deltas. Without the final event you have a partial answer that must not be shown or stored as complete.
- Partial structured output. JSON arrives in fragments; parsing is only valid at the end, unless you use an incremental parser designed for it.
- Usage only at the end. Token counts arrive in the final event (some APIs require an option to include them). If the stream breaks, usage is unknown.
- Client disconnects. If the user closes the tab, the server should stop consuming the stream to avoid paying for unused tokens.
- Latency metrics. Time to first token (TTFT) and total time are measured from stream events.
The assembler below processes a recorded stream, measures TTFT and detects an interrupted stream.
Run the code to see the result.
Output:
complete {'text': 'You can return unworn shoes within 30 days of delivery.', 'complete': True, 'time_to_first_token_s': 0.64, 'total_s': 0.79, 'usage': {'stop_reason': 'end', 'output_tokens': 12}}
broken {'text': 'You can return unworn shoes within ', 'complete': False, 'time_to_first_token_s': 0.64, 'total_s': 0.7, 'usage': None}
-> must not be shown as a full answer: retry or show an errorRecording real streams once and replaying them in tests, as in this example, is the standard way to test streaming code without network access.
Test cases for streaming
| Case | Expected behaviour |
|---|---|
| Normal stream | Text assembled completely; time to first token and total time recorded |
| Connection drops before the final event | Not shown or stored as a complete answer; the user can retry |
Final event reports max_tokens | Truncation handling runs |
| Tool-call deltas in the stream | Assembled into a complete tool call before execution |
| User closes the chat | The server stops reading the stream |
| Interrupted stream without usage data | Missing usage is logged |
| Error event in the middle of the stream | Handled like an API error |
| No event within the read timeout | Stream aborted cleanly |
Errors, retries and timeouts
Model APIs fail often enough that error handling is part of the core functionality. The status codes fall into two groups:
| Status | Meaning | Retry? |
|---|---|---|
| 400 | Invalid request, for example the input exceeds the context window | No: the same request will fail again; fix or shorten it |
| 401, 403 | Authentication or permission problem | No: alert, it is a configuration defect |
| 404 | Unknown model or endpoint, for example a retired model ID | No: alert |
| 413 | Request too large | No |
| 408, timeouts | Request or connection timed out | Yes, within the overall deadline |
| 429 | Rate limit exceeded | Yes, after the delay in the Retry-After header if present. Exception: a 429 that reports exhausted quota or billing problems will not recover by waiting |
| 500, 502, 503, 504 | Server or gateway error | Yes |
| 529 | Overloaded (Anthropic) | Yes |
A sound retry policy has five parts:
- Retry only retryable errors. Retrying a 400 wastes time and money and hides the defect.
- Exponential backoff with jitter. Wait about
base · 2^attemptseconds, capped, and randomize the wait ("full jitter": a random value between 0 and that bound) so that many clients do not retry in lockstep. - Respect `Retry-After` when the server provides it.
- Bound everything. A maximum number of attempts and an overall deadline that fits the user experience. Without them, a provider incident turns into requests that hang for minutes.
- Never blindly retry side effects. If a tool call already issued a refund, a retry of the whole turn must not issue it again. Use idempotency keys for side-effecting tools.
The implementation below follows these rules. Instead of real sleeping it uses a fake clock, which lets tests verify the waiting behaviour instantly; the scripted fake client plays back a sequence of failures and successes.
Run the code to see the result.
Output:
429 with Retry-After, 503, success calls=3 waited= 3.69s result=ok
400 bad request is not retried calls=1 waited= 0.00s result=raised HTTP 400
quota exhausted is not retried calls=1 waited= 0.00s result=raised HTTP 429 insufficient_quota
503 forever gives up after 5 tries calls=5 waited= 6.11s result=raised HTTP 503
Retry-After beyond the deadline calls=1 waited= 0.00s result=raised HTTP 429
all retry policy checks passedOfficial SDKs include their own retry logic with configurable limits. Know their defaults and make sure your own wrapper does not multiply them: an SDK that retries twice inside a wrapper that retries five times makes up to 15 attempts per user request, which can turn a brief provider problem into a self-inflicted overload.
Two more reliability mechanisms deserve tests:
- Timeouts at every level. Connection timeout, read timeout between stream events, and an overall deadline per user request.
- Fallback models. If the primary model is unavailable, some systems switch to another model. The fallback model must pass the same evaluation suite; otherwise the fallback path is an untested product.
Tokens, usage and cost
Providers charge per token, with separate prices for input and output tokens. Output tokens are usually several times more expensive. Cached input tokens (prompt caching: reusing an identical prompt prefix across requests) are discounted, and reasoning tokens are billed as output.
cost per request = (uncached input tokens × input price
+ cached input tokens × cached input price
+ output tokens (including reasoning) × output price) / 1,000,000Testers should own three cost measurements: cost per request, cost per conversation and, most important for the business, cost per resolved task. The model below uses example prices and a token budget measured from traces.
Run the code to see the result.
Output:
large-model: 4650 in / 220 out -> $0.01725 per request, $0.01293 with prompt caching
small-model: 4650 in / 220 out -> $0.00160 per request, $0.00124 with prompt caching
large-model: $0.0517 per resolved conversation, $2,070 per month
prompt growth of 1,500 tokens: +26% cost per requestTwo lessons follow. Retrieved context dominates the input in this budget, so retrieval settings (how many chunks, how long) are also cost settings. And an innocent-looking prompt edit can raise cost by a quarter; cost belongs in the regression report of every prompt change (Production quality).
Recording model calls for testing
You cannot test, debug or reproduce what you did not record. For every model call, log a record like this (with personal data redacted according to your privacy rules):
{
"trace_id": "3f9c1e7a",
"span": "answer_generation",
"timestamp": "2026-10-11T09:14:03Z",
"provider": "example-provider",
"model": "<pinned model id>",
"parameters": {"temperature": 0, "max_tokens": 500},
"prompt_template": {"id": "support-answer", "version": "v14"},
"messages_hash": "sha256:9b1d…",
"retrieved_chunk_ids": ["returns-policy#2", "returns-policy#5"],
"tools_offered": ["get_order_status"],
"tool_calls": [],
"stop_reason": "end",
"usage": {"input_tokens": 4650, "output_tokens": 220, "cached_input_tokens": 1600},
"latency_ms": {"time_to_first_token": 640, "total": 2310},
"retries": 0,
"output_text": "You can return unworn shoes within 30 days…",
"outcome": {"delivered": true, "user_feedback": null}
}Each field answers a testing question: which model and prompt produced this? (model, template version, parameters), what did it see? (messages hash, retrieved IDs, tools), why did it stop? (stop reason), what did it cost and how long did it take? (usage, latency) and what happened next? (outcome). These records become regression cases (Evaluation methodology and datasets), replay fixtures (Testing LLM code without the model) and the raw material of production monitoring (Production quality).
Worked example: an answer that quotes a replaced policy
Question from support: why did an answer quote a return policy that was replaced last week? The log record answers it field by field:
retrieved_chunk_idsshows which policy chunks the model saw; the ID of an old chunk means the index was not refreshed.timestampshows whether the call happened before or after the policy update and the reindex.prompt_template.versionandmodelexclude the prompt and the model as causes.output_textconfirms what exactly was quoted.- If the old chunk was not retrieved, the model reproduced the old policy from training data or from the conversation history; the stored history and
messages_hashshow what was in the context.
Choices that change testing
- Pin model versions. Providers offer dated snapshot IDs and moving aliases. Production should use a pinned snapshot, and moving to a new one is a migration with a full evaluation run.
- Track deprecations. Providers retire models on published schedules. A retired model ID returns an error (often 404). Keep a calendar of retirement dates for every model you use, including judge and fallback models.
- Local and OpenAI-compatible endpoints. Tools such as Ollama expose an OpenAI-compatible API for local models, which is useful for free practice and offline integration tests. Behaviour, limits and error formats still differ from the production provider, so they cannot replace tests against the real one.
- Gateways. A gateway or abstraction library normalizes providers, adds retries, budgets and logging in one place, and makes provider switches cheaper to test.
Example project
The method of this chapter is implemented and tested in examples/llm-testing/04-model-client: stop-reason handling for two providers, a retry policy tested with a fake clock, recorded streams replayed through the stream assembler, a cost model, and optional live checks against a real provider. All tests run offline, without a model or API keys:
uv sync --project examples/llm-testing
uv run --project examples/llm-testing pytest examples/llm-testing/04-model-clientCommon mistakes
- Delivering truncated output because the stop reason is never checked.
- Retrying non-retryable errors, or retrying without backoff, jitter, attempt limits or a deadline.
- Layered retries (SDK plus wrapper plus queue) that multiply attempts during incidents.
- Untested fallback models.
- Measuring cost per request only, not per conversation and per resolved task.
- Logging too little (no model version or prompt version) or too much (raw personal data without redaction and retention limits).
Summary
- An LLM application is mostly deterministic software around probabilistic model calls; test that software with conventional techniques.
- Chat APIs share one shape: messages with roles, parameters, content, stop reason and usage. Normalize provider differences at the edge.
- Every stop reason needs explicit handling and a test; truncated output must never be delivered as complete.
- Streaming needs tests for interruption, partial structured output, late usage data and latency measurement.
- Retry only retryable errors, with backoff, jitter, limits and deadlines; protect side effects; test with a fake clock.
- Cost is a quality attribute: measure it per request, per conversation and per resolved task.
- Record model, version, prompt version, parameters, context identifiers, stop reason, usage and latency for every call.