> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Model routing

# Smart Model Routing

*Never let a single point of failure kill your AI workflow*

AnyAPI's routing system ensures your applications stay resilient, fast, and cost-effective by intelligently managing model selection and fallbacks. Choose your strategy: let our AI pick the perfect model, or build bulletproof fallback chains.

## 🎯 Auto Router

*The smart choice that thinks for you*

Point your requests at `anyapi/auto` and watch as our AI-powered routing system automatically selects the optimal model for each prompt. No more guessing, no more manual optimization—just results.

```json theme={"system"}
{
  "model": "anyapi/auto",
  "messages": [
    {
      "role": "user", 
      "content": "Explain quantum computing to a 10-year-old"
    }
  ]
}
```

**What happens behind the scenes:**

* NotDiamond AI analyzes your prompt's complexity, style, and requirements
* Dynamically routes to the best-performing model for that specific task
* Returns the response with the actual model used in the `model` field

*Perfect for: Applications that need consistent quality without micromanagement*

## 🛡️ Fallback Chains

*Because reliability isn't optional*

Build unbreakable AI pipelines with the `models` parameter. Define your preferred model hierarchy and let AnyAPI handle the rest when things go sideways.

```json theme={"system"}
{
  "models": [
    "anthropic/claude-3.5-sonnet",    // First choice
    "openai/gpt-4o",                  // Solid backup
    "gryphe/mythomax-l2-13b"          // Always-available fallback
  ],
  "messages": [
    {
      "role": "user",
      "content": "Generate a product description for wireless headphones"
    }
  ]
}
```

**Your safety net activates when:**

* 🚫 Primary model hits rate limits
* ⏰ Provider experiences downtime
* 🛑 Content gets flagged by moderation
* 📏 Context length exceeds limits
* 💥 Any other model-specific errors

**The magic:** You only pay for the model that actually processes your request. No extra charges for the intelligent routing.

## 🔧 OpenAI SDK Integration

*Plug and play with your existing code*

Already using the OpenAI SDK? No problem. AnyAPI's routing works seamlessly with your current setup:

```typescript theme={"system"}
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://api.anyapi.ai/api/v1',
  apiKey: 'your-anyapi-key'
});

const completion = await client.chat.completions.create({
  model: 'openai/gpt-4o',
  models: [
    'anthropic/claude-3.5-sonnet', 
    'meta-llama/llama-3.1-70b-instruct'
  ],
  messages: [{
    role: 'user',
    content: 'Write a haiku about debugging code at 3 AM'
  }]
});

console.log(completion.choices[0].message.content);
```

## 💡 Pro Tips

**Mix and match strategies:** Use Auto Router for general tasks and fallback chains for mission-critical workflows.

**Strategic ordering:** Place your preferred models first in the array—AnyAPI tries them in sequence.

**Cost optimization:** Mix premium and budget models to balance quality and spend.

**Monitor model usage:** Check the returned `model` field to track which models are actually handling your requests.

***

*Ready to build bulletproof AI applications? Start routing smarter with AnyAPI.*
