Agent Model Resolution

How Agent() picks and routes an LLM when you omit llm= — from your environment

Agent() picks a model from your environment when you don’t pass llm=, then routes provider-prefixed defaults through litellm so a single provider key is enough to start.

Diagram

Agent Model Resolution

Yes

No

Yes

No

📋 Agent instructions=…

🔍 llm= passed?

⚙️ Has provider/prefix?

🔑 Scan env credentials

✅ litellm 100+ providers

✅ Native OpenAI client

As of PR #4795, the SDK Agent() constructor uses the same resolver as praisonai run — set one provider key and the Agent works with no llm=.

Quick Start

  1. Simplest — one provider key

    Set a single provider credential and construct an Agent with no llm=:

    python
    import os
    from praisonaiagents import Agent
    
    os.environ["OLLAMA_HOST"] = "http://localhost:11434"
    
    # No llm=, no OPENAI_API_KEY — the Agent picks ollama/llama3.2
    # and routes through litellm.
    agent = Agent(instructions="Answer briefly.")
    agent.start("2+2")
  2. Explicit provider-prefixed

    Pass a provider/model string to route through litellm:

    python
    from praisonaiagents import Agent
    
    agent = Agent(llm="anthropic/claude-3-5-sonnet-latest")
    agent.start("Write a haiku about the sea.")
  3. Bare OpenAI model

    A bare model name uses the native OpenAI client:

    python
    from praisonaiagents import Agent
    
    agent = Agent(llm="gpt-4o")
    agent.start("Summarise the plot of Hamlet.")

How It Works

The Agent scans your environment for a credential when llm= is omitted, then forks on whether the resolved model carries a provider/ prefix.

Diagram
OpenAI clientLiteLLMEnv scanAgentUserOpenAI clientLiteLLMEnv scanAgentUserBare best OpenAI chat model (OPENAI_API_KEY) → OpenAI clientAgent(instructions=”…”)Which credential is set?ollama/llama3.2 (OLLAMA_HOST)provider/model → route hereResponse

The first provider whose credential is present wins, in the order below. OpenAI is first so existing OpenAI users keep their default. The actual id is the capability-ranked best-available model for that provider at run time; the models below are shown as representatives — check praisonai models list --provider <p> for the current pick.

Credential env varDefault model class (ranked best-available at run time; prefix preserved)
OPENAI_API_KEYbest OpenAI chat model (e.g. gpt-4o)
ANTHROPIC_API_KEYanthropic/<best> (e.g. anthropic/claude-3-5-sonnet-latest)
GEMINI_API_KEYgemini/<best> (e.g. gemini/gemini-1.5-pro)
GOOGLE_API_KEYgoogle/<best> (e.g. google/gemini-1.5-pro)
GROQ_API_KEYgroq/<best> (e.g. groq/llama-3.3-70b-versatile)
COHERE_API_KEYcohere/<best> (e.g. cohere/command-r)
OLLAMA_HOSTollama/<best> (e.g. ollama/llama3.2)

Resolution Table

The class an Agent uses depends on the shape of the resolved model — a provider/ prefix routes through litellm, a bare name uses the native OpenAI client.

llm=EnvironmentResolved modelRouted through
omittedOPENAI_API_KEY onlyranked best OpenAI chat modelNative OpenAI client
omittedOLLAMA_HOST onlyollama/<best>litellm (100+ providers)
omittedANTHROPIC_API_KEY onlyanthropic/<best>litellm (100+ providers)
"gpt-4o"anygpt-4oNative OpenAI client
"anthropic/claude-3-5-sonnet-latest"anyas givenlitellm (100+ providers)
{"temperature": 0.5} (no model key)anydefaultNative OpenAI client

Before PR #4795, six of the seven env-var defaults resolved the right model name but were then sent to the native OpenAI client — the Agent raised ValueError: OPENAI_API_KEY environment variable is required. Only the bare OpenAI default worked. The fix routes any provider-prefixed default through litellm, matching praisonai run.


Common Patterns

I only have a Groq key

python
import os
from praisonaiagents import Agent

os.environ["GROQ_API_KEY"] = "gsk-..."

# Picks groq/llama-3.3-70b-versatile, routed through litellm.
agent = Agent(instructions="Answer fast.")
agent.start("Name three prime numbers.")

I have an Ollama server running

python
import os
from praisonaiagents import Agent

os.environ["OLLAMA_HOST"] = "http://localhost:11434"

# Picks ollama/llama3.2, routed through litellm — no cloud key needed.
agent = Agent(instructions="Answer locally.")
agent.start("Explain black holes to a 5-year-old.")

Best Practices

Prefer explicit llm= in production

Auto-detection is ideal for a first run. In production, pass llm="provider/model" so the model can’t change when you add or remove a provider key.

Keep OPENAI_MODEL_NAME bare unless you mean the override

OPENAI_MODEL_NAME wins over credential detection. A bare value like gpt-4o uses the OpenAI client; a provider/model value like ollama/llama3.2 now routes through litellm instead.

The SDK reads the same env vars as praisonai run

Agent() and praisonai run share one resolver. A single provider key gives a working Agent in Python and a working command on the CLI.


Related