Official Python SDK for Olbrain AI agents
Project description
Olbrain Python SDK
Official Python SDK for integrating Olbrain AI agents into your applications.
Installation
pip install olbrain-python-sdk
Quick Start
from olbrain import AgentClient
# Initialize client
client = AgentClient(
agent_id="your-agent-id",
api_key="sk_live_your_api_key"
)
# Create a session and send a message
session_id = client.create_session(title="My Chat")
response = client.send_and_wait(session_id, "Hello!")
print(response.text)
client.close()
Features
- Simple API - Just
agent_idandapi_keyto get started - Session Management - Create, update, archive sessions with metadata
- Sync & Streaming - Both request-response and real-time streaming
- Token Tracking - Monitor usage and costs per request
- Model Override - Switch models per-message
- Error Handling - Comprehensive exception hierarchy
Usage
Synchronous Messaging
from olbrain import AgentClient
with AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key") as client:
session_id = client.create_session()
response = client.send_and_wait(session_id, "What is Python?")
print(response.text)
print(f"Tokens: {response.token_usage.total_tokens}")
Real-Time Streaming
from olbrain import AgentClient
client = AgentClient(agent_id="your-agent-id", api_key="sk_live_your_key")
def on_message(msg):
print(f"[{msg['role']}]: {msg['content']}")
session_id = client.create_session(on_message=on_message)
client.send(session_id, "Tell me a story")
client.run() # Blocks and processes messages
Session Management
# Create session with metadata
session_id = client.create_session(
title="Support Chat",
user_id="user-123",
metadata={"source": "web"},
mode="production"
)
# Get session info
info = client.get_session(session_id)
print(f"Messages: {info.message_count}")
# Get message history
messages = client.get_messages(session_id, limit=20)
# Archive session
client.delete_session(session_id)
Model Override
response = client.send_and_wait(
session_id,
"Complex question here",
model="gpt-4" # Override default model
)
Error Handling
from olbrain import AgentClient
from olbrain.exceptions import (
AuthenticationError,
SessionNotFoundError,
RateLimitError,
OlbrainError
)
try:
client = AgentClient(agent_id="...", api_key="...")
response = client.send_and_wait(session_id, "Hello")
except AuthenticationError:
print("Invalid API key")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after}s")
except OlbrainError as e:
print(f"Error: {e}")
Configuration
Environment Variables
export OLBRAIN_API_KEY="sk_live_your_api_key"
export OLBRAIN_AGENT_ID="your-agent-id"
Logging
import logging
logging.basicConfig(level=logging.DEBUG)
API Reference
AgentClient
| Method | Description |
|---|---|
create_session() |
Create a new chat session |
send(session_id, message) |
Send message (async, use callback) |
send_and_wait(session_id, message) |
Send message and wait for response |
get_session(session_id) |
Get session details |
update_session(session_id, ...) |
Update session title/metadata |
delete_session(session_id) |
Archive a session |
get_messages(session_id) |
Get message history |
get_session_stats(session_id) |
Get token usage stats |
close() |
Clean up resources |
Response Objects
ChatResponse
text- Response textsuccess- Success statustoken_usage- TokenUsage objectmodel_used- Model that generated response
TokenUsage
prompt_tokens- Input tokenscompletion_tokens- Output tokenstotal_tokens- Total tokenscost- Cost in USD
Exceptions
| Exception | Description |
|---|---|
OlbrainError |
Base exception |
AuthenticationError |
Invalid API key |
SessionNotFoundError |
Session not found |
RateLimitError |
Rate limit exceeded |
NetworkError |
Connection issues |
ValidationError |
Invalid input |
StreamingError |
Streaming error |
Examples
See the examples/ directory:
basic_usage.py- Core SDK featuressession_management.py- Session CRUD operationsstreaming_responses.py- Real-time streamingerror_handling.py- Error handling patternsadvanced_features.py- Advanced usage
License
MIT License - see LICENSE
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
olbrain_python_sdk-0.2.1.tar.gz
(30.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file olbrain_python_sdk-0.2.1.tar.gz.
File metadata
- Download URL: olbrain_python_sdk-0.2.1.tar.gz
- Upload date:
- Size: 30.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
776f452d98edb46e731ba86dc0fa69eca322e7350b8f8a5f31ba00b51ca5db13
|
|
| MD5 |
1a90b3ab98a41b58d0750d7ee1c1e3a0
|
|
| BLAKE2b-256 |
87daeb6aab322af9851076c6b5257997bb93318da7ec073779ef41a80d7ae197
|
File details
Details for the file olbrain_python_sdk-0.2.1-py3-none-any.whl.
File metadata
- Download URL: olbrain_python_sdk-0.2.1-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e80395ad0670059677a1c1456f2dd0778ee807b55abbe1d40f09cfb9746d9e1b
|
|
| MD5 |
4e4448f6a44e7784f1cfaaad8c95a5d3
|
|
| BLAKE2b-256 |
5d26af378eda525cb4f5f7808a70c5912eebbe721e8a89736b1788997c90696f
|