LLM integration
Treating a language model as a slow, costly, non-deterministic dependency: one gateway module, timeouts, token budgets, injection and evals.
A well-read assistant who drafts answers. Slow, costly and sometimes wrong.
That is how an application should treat a large language model. It is useful, and it is also an outside party that takes seconds to answer, charges per word and can be confidently mistaken.
How it works
LLM integration is how an app calls a model such as Gemini, OpenAI or Claude as an external, slow, expensive and non-deterministic dependency. The safe pattern is one gateway module that every feature goes through, instead of provider calls scattered across the codebase.
Feature (chat, summary, extraction)
↓
LLM gateway: model choice · prompt template · token budget
timeout · retry · cost meter · logging
↓
Provider SDK (owned by one module)
Each feature asks the gateway for a result and states its purpose. The gateway records tokens in and out, checks the cost cap and returns the answer. Because one module owns the provider, swapping a model or a vendor does not touch feature code.
The concerns that matter
- Timeouts and retries. Set a hard timeout on every call. Retry only on rate-limit and server errors, with exponential backoff and jitter. Never retry a call that has already streamed output to the user.
- Token budgets. Cap the input by trimming history and limiting retrieved context. Cap the output with a maximum. Count tokens before the call where possible.
- Streaming. Sending tokens as they arrive makes the interface feel fast. It also makes errors harder: a failure can happen halfway, and the client can disconnect.
- Prompt injection. Treat user input and retrieved documents as data, not instructions. Keep system rules separate, restrict what tools the model may call, confirm before any write action and never put secrets in a prompt.
- Evals. Keep a set of saved questions with expected answers and score them on every prompt or model change. Without this, a prompt edit is a guess.
- Cost caps. Meter cost per tenant and stop or degrade at a cap. Rate-limit per tenant so one customer cannot burn a shared quota.
- Logging. Record model, latency, tokens, cost and finish reason, with personal data redacted.
Where a model does not belong
- Deterministic tasks that a rule, a regex or a SQL query solves reliably and cheaply.
- Hot paths with strict latency budgets, unless the result can be cached.
- Decisions with legal or financial weight, unless a person checks them.
Where I used it
Two of my secondary projects use models, and neither has a case study, so I keep the claims to what their project summaries say.
Spyra extracts structured data from specification files with an LLM and renders it to PDF. The work is confidential, so I say only that.
HealthBridge AI is an early-stage health assistant with retrieval over a vector store. It uses Groq, ElevenLabs, Qdrant and LangChain. Its stated problem is that a spoken answer has to arrive in seconds, so every retrieval step is a latency cost.
I make no claim here about gateways, eval sets or cost metering in either one.
Trade-offs
| Benefit | Cost |
|---|---|
| Flexible, powerful behaviour | Non-deterministic output, so you need evals |
| One gateway is one place for caps, logs and swaps | An extra layer of abstraction |
| Streaming improves the feel | Harder error handling and metering |
| Paid tiers give better data terms | Free tiers may use your data for training |
- Data terms matter. Read what the provider does with the prompts you send, especially for personal or client data.
- The model is a dependency that can be down. Decide what the feature does when the call fails or times out, and tell the user honestly.
- Quality drifts. A provider can change a model behind the same name. Evals are how you notice.
- Cost is easy to lose track of. A long conversation resends its whole history on each turn. Budgets stop that quietly growing.
A useful rule: if a rule or a query can do the job, use that. When you do call a model, call it through one door, with a time limit, a budget and a test set.