AIF-4Recommended
Call models through one module
Send every model call through one module of your own. Feature code never imports a provider's SDK directly.
How to do it
Give the module one function per kind of call, such as generate and embed. Put the model name, timeouts, retries, logging, cost tracking and fallbacks inside it. Feature code passes a prompt ID, the variables and an output schema. Then a provider change, a new log field or a spending cap is one change in one place. Inside the module, call each provider through its official SDK, as Use each provider's official SDK to call models says. A gateway product, such as LiteLLM or a cloud provider's AI gateway, fits only when you call two or more providers. It holds every provider key, so a hijacked release leaks them all. On 24 March 2026, two LiteLLM releases on PyPI carried malware that harvested credentials. Pin a gateway to an exact version with a hash, and give it keys with spending limits. It follows the rule to depend on abstractions at the edges.
Examples
Shell example
! grep -rnE "from ['\"](openai|@anthropic-ai/sdk|@google/genai)['\"]|^(from|import) (openai|anthropic|google\.genai)\b|^from google import genai" src/ --include=*.ts --include=*.py | grep -v "^src/ai/gateway"
TypeScript example
// src/ai/gateway.ts is the only file that knows the provider.
export async function generate<T>(request: { promptId: string; vars: Record<string, string>; schema: ZodType<T>; userId: string }): Promise<T> {
const prompt = await loadPrompt(request.promptId);
return withLimits(request.userId, () => callProvider(prompt.render(request.vars), request.schema));
}
Python example
# src/ai/gateway.py is the only file that knows the provider.
def generate(prompt_id: str, variables: dict[str, str], schema: type[BaseModel], user_id: str) -> BaseModel:
prompt = load_prompt(prompt_id)
return with_limits(user_id, lambda: call_provider(prompt.render(variables), schema))
How to check
A search for provider SDK imports outside the gateway module finds nothing, including the from google import genai form. A lint rule or a test runs that search in CI.
Sources
Related rules
- ARC-47 Depend on abstractions at the edges in Clean code and architecture
- STK-86 Use each provider's official SDK to call models in Software choices
- AIF-6 Keep prompts in files in AI features
- AIF-63 Route each kind of request to the model it needs in AI features
- AIF-47 Log every model call in AI features
- AIF-73 Connect product agents only to tools and servers on a reviewed list in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/deciding-to-use-a-model/model-gateway">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.