Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Generative AI & LLM · Chapter 06 / 20

Generative AI & LLM

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 metrics

Only 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).

ComponentDeterministic?Typical defectsMain test technique
API gatewayYesMissing auth, no size limits, no rate limitsAPI and security tests
OrchestratorYesLost or mixed-up conversation state, unbounded loops, no budgetsUnit and integration tests
Input guardrailsMostly (may call a classifier model)Over-blocking, under-blocking, PII leaking into promptsClassification metrics on labelled inputs
Context assemblyYesWrong template version, history window drops the system prompt, wrong user's dataUnit tests and snapshot tests of rendered prompts
Model clientWrapper yes, model noWrong stop-reason handling, retry storms, missing timeoutsFake-client tests, fault injection
Tool loopWrapper yesMissing authorization, unvalidated arguments, infinite loopsUnit tests, agent evaluation
Output parsingYesCrashes on invalid JSON, accepts invalid citationsUnit tests with recorded bad outputs
Output guardrailsMostlySame as input guardrailsClassification metrics
Model output qualityNoWrong, unfaithful, irrelevant or unsafe answersEvaluation 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"}}]
}
RolePurposeTesting note
system (or developer)Instructions from the application: persona, rules, formatMust survive history truncation; treat as public, never put secrets in it
userThe end user's messageUntrusted input
assistantEarlier model replies, or a prefilled start of the replyHistory you send back is also input; it can be manipulated if stored client-side
toolResult of a tool callUntrusted 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 meaningOpenAI Chat finish_reasonAnthropic stop_reasonCorrect application behaviour
Finished normallystopend_turn, stop_sequenceValidate and deliver
Hit the output limitlengthmax_tokens, model_context_window_exceededNever deliver as complete: retry with a larger limit, or shorten the request, or show a notice
Wants to call a tooltool_callstool_useExecute the tool (with authorization) and call the model again
Blocked or refusedcontent_filterrefusalShow a safe template and log for review
Must continuenot applicablepause_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.

Python
editable · browser sandbox
Result
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 reason

Streaming

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.

Python
editable · browser sandbox
Result
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 error

Recording 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

CaseExpected behaviour
Normal streamText assembled completely; time to first token and total time recorded
Connection drops before the final eventNot shown or stored as a complete answer; the user can retry
Final event reports max_tokensTruncation handling runs
Tool-call deltas in the streamAssembled into a complete tool call before execution
User closes the chatThe server stops reading the stream
Interrupted stream without usage dataMissing usage is logged
Error event in the middle of the streamHandled like an API error
No event within the read timeoutStream 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:

StatusMeaningRetry?
400Invalid request, for example the input exceeds the context windowNo: the same request will fail again; fix or shorten it
401, 403Authentication or permission problemNo: alert, it is a configuration defect
404Unknown model or endpoint, for example a retired model IDNo: alert
413Request too largeNo
408, timeoutsRequest or connection timed outYes, within the overall deadline
429Rate limit exceededYes, 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, 504Server or gateway errorYes
529Overloaded (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^attempt seconds, 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.

Python
editable · browser sandbox
Result
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 passed

Official 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,000

Testers 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.

Python
editable · browser sandbox
Result
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 request

Two 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_ids shows which policy chunks the model saw; the ID of an old chunk means the index was not refreshed.
  • timestamp shows whether the call happened before or after the policy update and the reindex.
  • prompt_template.version and model exclude the prompt and the model as causes.
  • output_text confirms 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_hash show 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-client

Common 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.

Source registry

11 chapter references
Messages API reference

Request and response shape of the Anthropic Messages API: system parameter, content blocks, usage fields.

Anthropic · official docs
Source ↗
Handling stop reasons

Authoritative list of stop_reason values (end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal, model_context_window_exceeded) and how to handle each.

Anthropic · official docs
Source ↗
API errors

HTTP error codes, including 429 rate limits and 529 overloaded, used for the retry policy.

Anthropic · official docs
Source ↗
Streaming messages

Server-sent event types for streamed responses and where usage and stop reasons appear.

Anthropic · official docs
Source ↗
Text generation guide

Request and response shape for the Responses and Chat Completions APIs.

OpenAI · official docs
Source ↗
Error codes

Error classes including rate-limit 429s versus insufficient-quota 429s that must not be retried.

OpenAI · official docs
Source ↗
Streaming API responses

Streaming event model and how to obtain usage for streamed responses.

OpenAI · official docs
Source ↗
Prompt caching

How cached input tokens are reported and discounted, used in the cost model.

OpenAI · official docs
Source ↗
Timeouts, retries, and backoff with jitter

Engineering reference for retry budgets, exponential backoff, jitter and the danger of layered retries.

Amazon Builders' Library · article
Source ↗
LiteLLM — unified interface to LLM providers

Gateway library that normalizes provider APIs, retries, budgets and logging.

BerriAI · tool
Source ↗
OpenAI compatibility

Local OpenAI-compatible endpoint for practice and offline integration tests.

Ollama · official docs
Source ↗