Skip to content
Try chatGet API key

Bayle as an agent backend

Most agent frameworks and assistants talk to an OpenAI-compatible chat completions endpoint. Bayle is one, so an agent that lets you set a custom provider can run on Bayle with three values:

Setting Value
Base URL https://api.bayle.ai
API key Your Bayle key, read from BAYLE_API_KEY
Model A model id from GET /models
Copies this page's setup instructions, to paste into your coding agent.
Show the prompt
Help me set this up to use Bayle AI, an OpenAI-compatible API. Full guide: https://docs.bayle.ai/guides/agents/ai-agents/

Goal: make my AI agent (or agent framework) use Bayle as its OpenAI-compatible chat completions provider.

1. Find where my agent takes a custom provider: a settings screen, a config file or a client constructor. If you cannot tell which agent this is from the project, ask me.
2. Set these three values:
   Base URL: https://api.bayle.ai
   API key: the value of BAYLE_API_KEY, through the agent's own environment-variable setting where it has one
   Model: MODEL_ID
   Bayle serves chat completions, embeddings, images and video only. Turn off any agent feature that needs the Responses API, the Assistants API or file uploads.
3. Pick a model that supports function calling:
   curl -s "https://api.bayle.ai/models?type=text" \
     | jq -r '.data[] | select(.model_spec.capabilities.supportsFunctionCalling) | .id'
   Show me the list, let me choose, and use that id as MODEL_ID. If the agent handles sensitive content, also show each model's model_spec.privacy.
4. If the agent loop is my own code: stream long replies with stream: true, retry 429, 503 and 504 with a backoff, and stop on 402 usd_spend_limit_exceeded instead of retrying.
5. Verify with a real request:
   curl -s https://api.bayle.ai/chat/completions \
     -H "Authorization: Bearer $BAYLE_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"model":"MODEL_ID","messages":[{"role":"user","content":"Reply with the word ready."}]}'
   Then run one short prompt through the agent itself.

Rules:
1. The base URL is https://api.bayle.ai exactly as written. Append no path to it.
2. Read the key from the BAYLE_API_KEY environment variable. Never write the key itself into a file you create, a command, a commit or the chat. Where the tool keeps the key in its own settings screen, tell me where to paste it. If BAYLE_API_KEY is not set, stop and ask me to create a key at https://chat.bayle.ai/developers and export it.
3. Take model ids only from GET https://api.bayle.ai/models (no key needed). Never guess one.
4. Finish with one real test request, show me the result and list the files you changed.
Don't have a Bayle key yet?Get API key

An agent needs a model that supports function calling. GET /models needs no key, and each model’s model_spec.capabilities says what it can do:

Terminal window
curl -s "https://api.bayle.ai/models?type=text" \
| jq -r '.data[] | select(.model_spec.capabilities.supportsFunctionCalling) | .id'

The same object has supportsReasoning, supportsVision and optimizedForCode, which are worth checking for a coding or multimodal agent. If your agent handles sensitive content, also check model_spec.privacy; see Privacy.

Send tools the OpenAI way. When the model wants to call one, finish_reason is tool_calls and the calls are in message.tool_calls.

import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["BAYLE_API_KEY"], base_url="https://api.bayle.ai")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
response = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "What's the weather in Recife?"}],
tools=tools,
)
call = response.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)

Run the tool, append its result as a tool message with the matching tool_call_id, and call again. Text generation has the full loop.

Set stream: true to receive tokens as they are generated. Agents that show progress, or that run long tasks, should stream: a non-streamed completion must finish within 30 seconds, while a stream has no deadline of Bayle’s own.

stream = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "Plan a three-step refactor."}],
stream=True,
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)

If a stream fails partway, the last event is an error envelope followed by [DONE]. The OpenAI SDKs raise on it; a hand-written parser should check each event for an error key. See How Bayle works.

  • Retries. Bayle never retries a chat completion for you, because a retry could be billed twice. Retry 429, 503 and 504 in your agent with a backoff; see Errors.
  • Balance. When your key runs out of balance, requests fail with 402 usd_spend_limit_exceeded. An agent loop should stop on it rather than retry.
  • Unknown fields. Parameters Bayle doesn’t name are passed to the model as sent, so framework-specific options generally reach it.
  • Endpoints. Only chat completions, embeddings, images and video are served. An agent feature that needs the Responses API, the Assistants API or file uploads won’t work.