LLM Application Architecture
Overview
LLM application architecture focuses on building reliable AI features around a model. The model is only one component. Retrieval, caching, safety checks, and observability are equally important.
Reference Architecture
flowchart LR
A[Client] --> B[API Gateway]
B --> C[Application Service]
C --> D[Prompt Builder]
C --> E[Retrieval Service]
D --> F[LLM Provider]
E --> G[Vector Store]
C --> H[Policy and Guardrails]
C --> I[Telemetry]
Practical Example - Service Composition in C#
public interface IAnswerService
{
Task<string> AnswerAsync(string question, CancellationToken ct);
}
public sealed class AnswerService : IAnswerService
{
private readonly IRetrievalService _retrieval;
private readonly ILlmClient _llm;
public AnswerService(IRetrievalService retrieval, ILlmClient llm)
{
_retrieval = retrieval;
_llm = llm;
}
public async Task<string> AnswerAsync(string question, CancellationToken ct)
{
var context = await _retrieval.GetContextAsync(question, ct);
var prompt = $"Use only the context below.\\n{context}\\nQuestion: {question}";
return await _llm.CompleteAsync(prompt, ct);
}
}
Production Example - Failure Handling
Production systems should include:
- Timeout and retry policies for provider calls
- Fallback responses when retrieval is empty
- Rate limiting and per-tenant quotas
- Correlation IDs across request, retrieval, and model inference
Common Pitfalls
- Treating the model as the entire architecture
- No strategy for context freshness
- Missing latency budgets per component
Key Takeaways
- AI applications are distributed systems.
- Retrieval, safety, and telemetry are first-class concerns.
- Architecture should assume model and provider failures.