On this page12
- At a glance
- What the layer does
- Why one layer instead of calling each provider
- How one request travels through it, step by step
- In production: the router behind socialMedia V4
- The model is an untrusted service
- Choosing a model under real limits
- Agents and tools
- Many applications, one layer
- Frequently asked questions
- See it in real products
- Need AI inside an application you already run?
Part of AI engineering, intelligent automation and production systems. By Mohammad Javad Dastouri, full-stack and AI engineer.
My production LLM infrastructure is about 3,600 lines of integration code. Applications talk to one layer, and that layer talks to every AI provider.
- lines of integration code
- 3,600
- providers behind one interface
- 5
- AI calls logged by one router in 3 weeks
- 859
At a glance
- Providers
- OpenAI, Anthropic Claude, Google Gemini, Cohere and AWS Bedrock, plus n8n AI workflow integrations
- Size
- About 3,600 lines of integration code
- Design
- One unified interface, with an adapter per provider
- Controls
- Encrypted API keys, usage limits, token tracking, fallback, validated output and approval steps
- Used by
- AI agents, automation workflows and existing business applications
- Providers
- OpenAI, Anthropic Claude, Google Gemini, Cohere and AWS Bedrock, plus n8n AI workflow integrations
- Size
- About 3,600 lines of integration code
- Design
- One unified interface, with an adapter per provider
- Controls
- Encrypted API keys, usage limits, token tracking, fallback, validated output and approval steps
- Used by
- AI agents, automation workflows and existing business applications
What the layer does
Multi-provider routing
Each task goes to the provider and model that suits it.
Provider abstraction
An adapter per provider, so the application never sees provider differences.
AI agents and tools
Agents call tools through controlled execution.
Encrypted key management
Provider API keys are stored encrypted.
Usage limits
Limits are checked before a call is made.
Token tracking
Tokens used are recorded per call, so cost stays visible.
Provider fallback
A failing provider does not stop the request.
Structured output
Responses are validated against the shape the application expects.
Approval workflows
Important actions wait for a person to approve them.
Multi-provider routing
Each task goes to the provider and model that suits it.
Provider abstraction
An adapter per provider, so the application never sees provider differences.
AI agents and tools
Agents call tools through controlled execution.
Encrypted key management
Provider API keys are stored encrypted.
Usage limits
Limits are checked before a call is made.
Token tracking
Tokens used are recorded per call, so cost stays visible.
Provider fallback
A failing provider does not stop the request.
Structured output
Responses are validated against the shape the application expects.
Approval workflows
Important actions wait for a person to approve them.
Why one layer instead of calling each provider
An application can change provider without being rewritten.
Keys, limits and token costs are managed in one place.
An outage at one provider does not stop the system.
Every provider answers in the same checked shape.
An application can change provider without being rewritten.
Keys, limits and token costs are managed in one place.
An outage at one provider does not stop the system.
Every provider answers in the same checked shape.
How one request travels through it, step by step
One request: The application or agent sends a single request format, whatever the provider.
Routing: The layer chooses the provider and model for that task.
Keys and limits: The API key comes from encrypted storage, and usage limits are checked before the call.
Provider adapter: The adapter turns the request into that provider's own API call.
Fallback: If the provider fails, the request moves to the next one.
Token tracking: Tokens used are recorded per call.
Validation: Structured output is checked; a wrong answer is corrected or refused.
Approval: Actions that change something important wait for a person to approve them.
One request: The application or agent sends a single request format, whatever the provider.
Routing: The layer chooses the provider and model for that task.
Keys and limits: The API key comes from encrypted storage, and usage limits are checked before the call.
Provider adapter: The adapter turns the request into that provider's own API call.
Fallback: If the provider fails, the request moves to the next one.
Token tracking: Tokens used are recorded per call.
Validation: Structured output is checked; a wrong answer is corrected or refused.
Approval: Actions that change something important wait for a person to approve them.
In production: the router behind socialMedia V4
My AI marketing system, socialMedia V4, cannot stop publishing because one provider has an outage or an empty balance. Its router ranks OpenAI, Anthropic, DeepSeek, Gemini and xAI automatically.
Ranks the 5 providers by first-pass quality, speed and cost.
Takes a failing provider out of rotation and tests it again before bringing it back.
Uses an automation fallback as the last tier.
Rejects messy output and retries, instead of cleaning it up and shipping it.
Logged 859 AI calls in its first three weeks, each with its provider, cost and outcome.
Ranks the 5 providers by first-pass quality, speed and cost.
Takes a failing provider out of rotation and tests it again before bringing it back.
Uses an automation fallback as the last tier.
Rejects messy output and retries, instead of cleaning it up and shipping it.
Logged 859 AI calls in its first three weeks, each with its provider, cost and outcome.
The model is an untrusted service
A model's output is not guaranteed to match anything. So everything it returns passes through one boundary before any consumer sees it.
Defaults first: The output is merged onto safe defaults, so every field exists before the model answers.
Types: Booleans must be booleans, and numbers must be numbers.
Allowed values: Fields with a fixed set of values accept only those; anything else becomes empty.
Same shape on failure: Malformed output or an unavailable model still returns the same response shape, with a fallback flag.
Defaults first: The output is merged onto safe defaults, so every field exists before the model answers.
Types: Booleans must be booleans, and numbers must be numbers.
Allowed values: Fields with a fixed set of values accept only those; anything else becomes empty.
Same shape on failure: Malformed output or an unavailable model still returns the same response shape, with a fallback flag.

Choosing a model under real limits
In my Google Search Intelligence API, the web server allowed 60 seconds per request, and that limit could not be changed. The AI step had to fit inside it with seven other stages.
grok-3
- Time for the AI step
- About 35 seconds
- Result
- Too slow beside the other stages
grok-3-mini
- Time for the AI step
- About 8 to 12 seconds
- Result
- Chosen
grok-3
- Time for the AI step
- About 35 seconds
- Result
- Too slow beside the other stages
grok-3-mini
- Time for the AI step
- About 8 to 12 seconds
- Result
- Chosen
The binding limit in production is often the infrastructure, not the model. Choosing the smallest model that does the job also keeps cost down.
Agents and tools
Agents use the same layer. They call tools through controlled execution: each tool has a defined purpose, and the agent cannot step outside it. I also connect agents to real tools through MCP, the Model Context Protocol. How I audit agents.
Many applications, one layer
When several teams or applications need AI, they all connect to the same layer. Keys, limits, token tracking and fallback are set up once, and every new connection uses them from its first call.
How bulk orders work, step by step.
Frequently asked questions
Can we keep our current AI provider?
Yes. The layer adds providers behind one interface; it does not force you to drop the one you use.
Where are the API keys kept?
In encrypted storage, used by the provider adapters. Applications never handle the keys themselves.
How do we know what AI costs us?
Tokens are recorded per call, so usage can be reported per provider and per task, and limits stop spending beyond what you set.
What if the model returns nonsense?
The output is checked against the expected structure and values. A failing answer is corrected, retried or refused; it is not passed on.
See it in real products
The AI router in socialMedia V4
Ranks providers, skips a failing one and logs every call.
An output contract for AI
Raw model output turned into the same checked response shape.
Need AI inside an application you already run?
Tell me which systems it has to work with. Next: the agents and automation built on this layer.
- Section
- Services
- Reading time
- 5 min read
Hiring for this kind of work?
Tell me about the role and I will reply by email.
Discuss a role with me
