Sub topic 12
What the abstraction cannot hide
This chapter assumes you have read the earlier chapters on neutral types, the provider interface and adapters. It covers the differences a shared interface cannot remove, and how to handle them in code.
Where the abstraction stops
An adapter translates shapes: field names, message layout, error classes. It cannot translate behavior. Two providers can accept the same ChatRequest and return different answers, at different speeds, with different failure modes. If you treat the interface as a guarantee of equivalence, you will get surprises in production.
Model behavior
The same prompt produces different output on different models. Common differences:
- Instruction following. One model obeys a formatting rule that another ignores or interprets loosely.
- Style and length. Verbosity, refusals and tone vary, so downstream parsing that works on one model may break on another.
- Sampling. A
temperatureof 0.7 does not mean the same thing across vendors, and some models restrict or ignore it. - Tokenization. The same text costs a different number of tokens. A
max_tokensvalue that fits one model's output may truncate another's.
No adapter can fix these. The practical response is an evaluation set: a fixed list of prompts with checks on the output. Run it against each provider you intend to switch to before you change the configuration.
Feature support
Streaming, tool calls, structured output, image input and system-prompt handling are not uniform. A neutral type can only carry the features that every provider shares, or it must carry optional fields that some adapters ignore.
Silently ignoring an unsupported field is the worst option. The caller believes it asked for JSON output or a tool call and gets plain text. Prefer to make the gap explicit.
from dataclasses import dataclass
@dataclass(frozen=True)
class Capabilities:
max_context_tokens: int
supports_tools: bool
supports_streaming: bool
class UnsupportedFeature(Exception):
pass
class FakeProvider:
capabilities = Capabilities(
max_context_tokens=8_000,
supports_tools=False,
supports_streaming=True,
)
def require_tools(provider) -> None:
if not provider.capabilities.supports_tools:
raise UnsupportedFeature(
f"{type(provider).__name__} does not support tool calls"
)
try:
require_tools(FakeProvider())
except UnsupportedFeature as err:
print(err)Each adapter declares what it supports, and calling code checks before it relies on a feature. Raise UnsupportedFeature instead of degrading quietly.
Limits, pricing and operations
These differences sit outside the request and response entirely:
| Difference | Why it leaks |
|---|---|
| Context window | A prompt that fits one model is rejected by another. |
| Rate limits | Retry timing and safe concurrency differ per provider and account. |
| Latency | A fallback target may be much slower, which affects timeouts. |
| Price | Switching changes cost per request, sometimes by an order of magnitude. |
| Data handling | Retention, region and compliance terms differ and need review. |
Fallback makes this worse. If your primary provider has a large context window and the fallback has a small one, a request can succeed on the first and fail on the second only when you are already in an outage. Test the fallback path with your largest realistic prompts.
Decision criteria
Use the lowest common denominator when the features are truly interchangeable, such as plain text completion for summaries. Expose provider-specific options through an escape hatch, such as an extra: dict field that only the matching adapter reads, when one provider has a feature you need. Accept that some code paths are tied to one provider, and name them that way.
The more of a provider's distinctive features you use, the less the interface buys you. At that point, switching costs come from prompts, evaluations and tool definitions, not from SDK calls.
Exercise
Your application switches from provider A to provider B by changing LLM_PROVIDER. All tests pass against the fake provider, but users report that a JSON-parsing step now fails. Name two causes the abstraction cannot prevent, and one step that would have caught the problem.
Show answer
Likely causes are that model B formats or wraps JSON differently (a behavior difference), or that it does not support the structured-output option the adapter ignored (a feature gap). The fake provider cannot show either. An evaluation set run against provider B before the switch, checking that the output parses, would have caught it.