# Provider-String Routing

> Select the model and provider for a request through a single namespaced string (`provider/model`) backed by env-var credentials, so the caller spec...

- **Category**: Agentic AI
- **Subcategory**: Routing & Composition
- **Canonical URL**: https://designpattern.fyi/patterns/provider_string_routing/

---

## Description
**Intent**: Select the model and provider for a request through a single namespaced string (`provider/model`) backed by env-var credentials, so the caller specifies what to run with one parameter rather than a typed provider object.
**Context**: A team is building an application that needs to talk to several language-model providers and many model variants — OpenAI, Anthropic, Google, xAI, OpenRouter, and others — possibly choosing between them on a per-request basis for cost lanes, experiments, or tenant-specific routing. The application is otherwise model-agnostic; it does not need to depend on the typed object hierarchy of any one provider's software development kit. The team controls the call sites where each model invocation happens.
**Solution**: Define a unified language-model interface and a registry of providers keyed by short prefix (`openai/`, `anthropic/`, `google/`, `xai/`, `openrouter/...`). Each provider implementation knows how to read its credentials from environment variables. The call site takes a single string (`'anthropic/claude-sonnet-4-6'`) and the runtime resolves provider, credentials, and capability flags. Pair with provider-fallback (chain strings for resilience), multi-model-routing (pick a string by quality/cost), and vendor-lock-in (this is its mirror — the un-locked version).


## Use Cases
- The application targets multiple providers and may change the mix over time.
- Per-call routing (experiments, A/B, cost lanes) shares a single call site.
- Credentials are managed by environment, not by application code.
- A central capability registry is acceptable to track which providers support which features.





## Trade-offs


### Advantages

- Switching provider is a string change.

- Per-call experiments and A/B routing share a single call site.

- Configuration moves out of code into environment.

- Composable with provider-fallback and multi-model-routing without further abstraction.




### Considerations & Drawbacks

- String typing loses compile-time checking of valid provider/model combinations.

- Per-provider capability gaps must be discoverable at runtime, not at type-check time.

- Misspelled identifiers fail at runtime rather than at edit time.

- Credential rotation depends on the env-var convention being consistent across providers.







---
**Reference**: [Original Source](https://www.agentpatternscatalog.org/patterns/provider-string-routing/)

