Developers
Build AI agents in Python
Agents call tools in a loop — searching, reading files, running code — so a single task can take dozens of requests. Tool-calling reliability and total cost per task matter most. Below is a working Python starting point using the official openai Python package.
Python example
python
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["SOMNUS_API_KEY"],
base_url="https://gateway-production-c837.up.railway.app/v1",
)
resp = client.chat.completions.create(
model="claude-sonnet-5",
messages=[
{"role": "user", "content": "What's the weather in Lisbon right now?"}
],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather for a city",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
},
}],
)
print(resp.choices[0].message.tool_calls)Models that fit
| Model | Maker | List price (in / out per 1M) | Through Somnus | Why |
|---|---|---|---|---|
| Claude Sonnet 5 | Anthropic | $2 / $10 | $1 / $5 | reliable tool use at a mid price |
| Claude Opus 5.5 | Anthropic | $4 / $20 | $2 / $10 | long, complex tasks |
| GPT-5.6 Sol | OpenAI | $5 / $30 | $2.5 / $15 | alternative strong planner |
Somnus column uses the minimum 2× credit; larger top-ups go further.
Making it production-ready in Python
- Read the key from an environment variable, never hard-code it.
- Use the async client (AsyncOpenAI) for web servers.
- Give each tool a precise name and JSON schema.
- Stop the loop after a fixed number of steps.
Avoid
- Unbounded loops that keep calling tools.
- Vague tool descriptions that cause wrong calls and retries.
Questions
Do I need a special SDK for AI agents in Python?
No — the official openai Python package is enough, because Somnus uses the OpenAI chat-completions format.
Run this code with free credit
Create an account and get $5 of free API credit — enough to try every example on this page.
Get $5 free creditRelated
Browse more: Alternatives · Compare · Use cases · Developers · Solutions · Tools