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

# LLM Config

> Configure primary and fallback LLM models with provider prefixes and API settings

LLM Config lets you set the model, API endpoint, and fallback chain so your agent always has a working model.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.config import LLMConfig

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant.",
    llm_config=LLMConfig(
        model="gpt-4o",
        fallback_models=["claude-3-5-sonnet-20241022", "gpt-4o-mini"],
    ),
)

result = agent.start("Explain quantum computing simply")
print(result)
```

The user sends a prompt; the agent tries the primary model and transparently falls back if the provider fails.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "LLM Configuration"
        AGENT["🤖 Agent"] --> PRIMARY["⭐ Primary
Model"]
        PRIMARY -- Fails --> FALLBACK1["🔄 Fallback 1"]
        FALLBACK1 -- Fails --> FALLBACK2["🔄 Fallback 2"]
        PRIMARY -- Success --> RESP["✅ Response"]
        FALLBACK1 -- Success --> RESP
        FALLBACK2 -- Success --> RESP
    end

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef primary fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef fallback fill:#F59E0B,stroke:#7C90A0,color:#fff
    classDef success fill:#10B981,stroke:#7C90A0,color:#fff

    class AGENT agent
    class PRIMARY primary
    class FALLBACK1,FALLBACK2 fallback
    class RESP success
```

## Quick Start

<Steps>
  <Step title="Primary Model Only">
    Set just the model to get started:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent
    from praisonaiagents.config import LLMConfig

    agent = Agent(
        name="Assistant",
        instructions="You are a helpful assistant.",
        llm_config=LLMConfig(model="gpt-4o")
    )

    agent.start("What is the speed of light?")
    ```
  </Step>

  <Step title="With Fallback Chain">
    Add fallback models for reliability — if the primary fails, the agent automatically tries the next:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent
    from praisonaiagents.config import LLMConfig

    agent = Agent(
        name="ResilientAssistant",
        instructions="You are a helpful assistant.",
        llm_config=LLMConfig(
            model="gpt-4o",
            fallback_models=["claude-3-5-sonnet-20241022", "gpt-4o-mini"],
        ),
    )
    agent.start("Summarize today's top AI research papers.")
    ```
  </Step>

  <Step title="With LiteLLM Provider Prefix">
    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent
    from praisonaiagents.config import LLMConfig

    agent = Agent(
        instructions="You are a helpful assistant.",
        llm_config=LLMConfig(
            model="anthropic/claude-3-5-sonnet-20241022",
            fallback_models=["openai/gpt-4o", "openai/gpt-4o-mini"],
        ),
    )
    agent.start("Write a technical blog post about microservices.")
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant LLM as Primary LLM
    participant Fallback as Fallback LLM

    User->>Agent: Request
    Agent->>LLM: API call
    alt Success
        LLM-->>Agent: Response
    else Error / Rate limit
        Agent->>Fallback: Retry with fallback
        Fallback-->>Agent: Response
    end
    Agent-->>User: Response
```

| Phase       | What happens                                               |
| ----------- | ---------------------------------------------------------- |
| 1. Primary  | Agent calls the configured `model` first                   |
| 2. Fallback | On failure, tries each model in `fallback_models` in order |
| 3. Response | First successful response is returned                      |

***

## Configuration Options

<Card title="LLMConfig SDK Reference" icon="code" href="/docs/sdk/reference/python/classes/LLMConfig">
  Full parameter reference for LLMConfig
</Card>

| Option            | Type                | Default      | Description                    |
| ----------------- | ------------------- | ------------ | ------------------------------ |
| `model`           | `str`               | *(required)* | Primary model to use           |
| `fallback_models` | `list[str] \| None` | `None`       | Ordered fallback models        |
| `base_url`        | `str \| None`       | `None`       | API endpoint override          |
| `api_key`         | `str \| None`       | `None`       | API key (defaults to env vars) |
| `auth`            | `dict \| None`      | `None`       | Additional auth headers        |

***

## Common Patterns

### Pattern 1 — Self-hosted model via custom endpoint

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.config import LLMConfig

agent = Agent(
    instructions="You are a helpful assistant.",
    llm_config=LLMConfig(
        model="llama3.2",
        base_url="http://localhost:11434/v1",
        api_key="ollama",
    ),
)
response = agent.start("What is the capital of France?")
print(response)
```

### Pattern 2 — Multi-provider fallback for resilience

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.config import LLMConfig

agent = Agent(
    name="PrivateModelAgent",
    instructions="You are a helpful assistant.",
    llm_config=LLMConfig(
        model="my-custom-model",
        base_url="https://my-llm-gateway.example.com/v1",
        api_key="sk-my-private-key",
        auth={"X-Organization-ID": "org-123"},
    )
)
```

**Self-hosted model via Ollama:**

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.config import LLMConfig

agent = Agent(
    name="LocalAgent",
    instructions="You are a helpful assistant.",
    llm_config=LLMConfig(
        model="ollama/llama3.2",
        base_url="http://localhost:11434",
    )
)
```

### Pattern 3 — Multi-provider fallback for business continuity

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonaiagents.config import LLMConfig

agent = Agent(
    name="ResilientAgent",
    instructions="You are a reliable assistant for critical tasks.",
    llm_config=LLMConfig(
        model="openai/gpt-4o",
        fallback_models=[
            "anthropic/claude-3-5-sonnet-20241022",
            "openai/gpt-4o-mini",
        ],
    ),
)
agent.start("Generate a business continuity plan for our e-commerce platform.")
```

***

## Best Practices

<AccordionGroup>
  <Accordion title="Always add at least one fallback model">
    Even one fallback prevents your agent from failing completely during rate limits or outages. Use a smaller, cheaper model as the fallback — it costs less and is often available when the primary isn't.
  </Accordion>

  <Accordion title="Use environment variables for API keys">
    Never hardcode `api_key` in your code. Leave it as `None` and set `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc. in your environment instead. `LLMConfig` picks them up automatically via LiteLLM.
  </Accordion>

  <Accordion title="Use LiteLLM prefixes for multi-provider setups">
    Prefix models with the provider name (`anthropic/`, `openai/`, `ollama/`) to avoid ambiguity when using multiple providers in a fallback chain.
  </Accordion>

  <Accordion title="Test your fallback chain">
    Deliberately trigger a failure (e.g., invalid primary model name) and verify your fallback fires correctly before going to production.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="LLM Gateways" icon="plug" href="/docs/features/llm-gateways">
    Route requests through a unified LLM gateway
  </Card>

  <Card title="LLM Endpoint Config" icon="settings" href="/docs/features/llm-endpoint-config">
    Fine-tune endpoint parameters per model
  </Card>

  <Card title="LLM Error Classification" icon="triangle-alert" href="/docs/features/llm-error-classification">
    How errors trigger fallback in the chain
  </Card>

  <Card title="Failover" icon="shield" href="/docs/features/failover">
    Automatic failover strategies for reliability
  </Card>
</CardGroup>
