Coco
Build agents

Models

Pick a model with a provider string or a model class, switch between OpenAI and Anthropic, and configure endpoints, retries and timeouts.

Pick a model, switch providers, and configure each one.

Prerequisites: The framework with the openai or anthropic extra, and that provider's key. See Install.


Name a model with a provider string

create_agent and init_chat_model take a "<provider>:<model>" string, so you switch models without changing imports:

from coco.agents import create_agent
from coco.chat_models import init_chat_model

# Fixed model initialization
gpt = init_chat_model("openai:gpt-4o", temperature=0.2)
claude = init_chat_model("anthropic:claude-sonnet-4-5-20250929", max_tokens=1024)

# Or pass the model string directly into create_agent
agent = create_agent(model="anthropic:claude-sonnet-4-5-20250929", tools=[])

Choose the model at run time

With configurable_fields, each call picks the model through its config:

from coco.chat_models import init_chat_model

configurable_llm = init_chat_model(configurable_fields=("model", "model_provider", "temperature"))
res = configurable_llm.invoke(
    "Summarize this paragraph.",
    config={"configurable": {"model": "gpt-4o", "model_provider": "openai", "temperature": 0}},
)

Configure OpenAI

from coco_openai import ChatOpenAI, OpenAIEmbeddings

llm = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    max_retries=3,
    timeout=30.0,
    # base_url="https://your-openai-compatible-gateway.internal/v1",
)

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector = embeddings.embed_query("Coco agent runtime specification")

ChatOpenAI retries HTTP 429 and 5xx errors up to max_retries times, 2 by default, with exponential backoff. base_url, or the OPENAI_BASE_URL variable, points it at an OpenAI-compatible endpoint.

Keep in mind: For models whose names start with gpt-6, a call with tools goes to the Responses API, /v1/responses, not Chat Completions. Point those models only at an endpoint that serves the Responses API, or the call fails.

Configure Anthropic

from coco_anthropic import ChatAnthropic

claude = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    temperature=0,
    max_tokens=2048,
    max_retries=3,
    default_headers={"anthropic-beta": "prompt-caching-2024-07-31"},
)

coco_anthropic reads ANTHROPIC_API_KEY, and ANTHROPIC_API_URL sets a custom endpoint.

Good to know: To survive a provider's outage or rate limits across providers, add ModelRetryMiddleware and ModelFallbackMiddleware.


On this page