Sub topic 2
What Changes When the Model Is a Dependency
This chapter assumes you understand training, validation, deployment, basic Python, and service logging. You should also know that a large language model (LLM) generates text from a prompt and that an application can call an LLM through an API.
In classic machine learning operations (MLOps), your team often owns the model artifact. You train it, validate it, package it, deploy it, and monitor its predictions. In an LLM application, a hosted pretrained model is often an external dependency. Your application controls the instructions, inputs, surrounding code, and output handling, while another provider controls the model runtime.
The anatomy of an LLM application
A useful working model is a pipeline:
- An application receives a user request.
- It validates the request and applies access rules.
- It retrieves relevant data, if the application uses retrieval-augmented generation (RAG).
- It assembles a prompt from instructions, context, and user content.
- It calls the model provider.
- It validates, transforms, and stores or displays the response.
- It records traces, costs, errors, and user feedback.
The model is only one component. Prompt templates, retrieval code, document indexes, model settings, parsers, tools, and policies can all change the result.
What your team owns
| Component | Typical responsibility | Common failure |
|---|---|---|
| Prompt | Define the task, constraints, and response format | Ambiguous instructions |
| Retrieval | Select useful supporting context | Irrelevant or stale documents |
| Model configuration | Select model, temperature, and token limits | Unexpected behavior or cost |
| Output handling | Parse and validate generated content | Invalid JSON or unsafe text |
| Operations | Trace requests and monitor quality | No way to explain a bad answer |
Classic MLOps concerns still apply. You need repeatable deployments, access control, monitoring, rollback plans, and evaluation data. The boundary of ownership changes, however. You may not be able to inspect model weights, reproduce the provider's serving environment, or prevent a provider from changing a model alias.
A model call is an application boundary
Treat the provider call like a dependency boundary, similar to a database or payment service. Hide provider-specific details behind an adapter. The adapter can normalize responses, count tokens, handle retries, and expose a stable interface to the rest of the application.
from dataclasses import dataclass
@dataclass
class ModelResult:
text: str
input_tokens: int
output_tokens: int
class FakeModelClient:
def generate(self, prompt: str) -> ModelResult:
text = f"Summary: {prompt.strip()}"
return ModelResult(text, len(prompt.split()), len(text.split()))
def summarize(ticket: str, client: FakeModelClient) -> ModelResult:
prompt = (
"Summarize the support ticket in one sentence.\n\n"
f"Ticket: {ticket}"
)
return client.generate(prompt)
result = summarize("My invoice shows a duplicate charge.", FakeModelClient())
print(result.text)
print(f"Tokens: {result.input_tokens} in, {result.output_tokens} out")This boundary makes model replacement easier, but it does not make providers interchangeable automatically. Models differ in instruction-following, context limits, latency, pricing, safety behavior, and output style. Test a replacement against your real evaluation cases before switching it.
A practical ownership checklist
For each request path, identify:
- Which input is trusted and which input can contain untrusted instructions.
- Which data enters the prompt and whether sensitive data is allowed.
- Which model identifier and settings are active.
- Which output fields must pass validation before use.
- Which events are logged for debugging without exposing private content.
- Which component can be rolled back independently.
The result is an application whose behavior comes from several coordinated artifacts, not from the model alone.
Exercise
Why should an LLM call be hidden behind an adapter instead of being scattered through application code?
Show answer
An adapter centralizes provider-specific behavior such as response normalization, token accounting, retries, and model selection, making the application easier to test and change.