A progressive-disclosure Python library for LLM APIs
Project description
Aigent
A progressive-disclosure Python library for LLM APIs. Start with a one-liner, scale to full control — all in idiomatic Python.
from aigent import Aigent
agent = Aigent(api_type="openai")
with agent.session() as s:
print(s.chat("Hello!"))
Installation
pip install uss-aigent
Requires Python 3.10+. The only external dependency is httpx.
Quick Start
Tier 1 — Chat
from aigent import Aigent
# Zero-config: reads OPENAI_API_KEY / ANTHROPIC_API_KEY from env
agent = Aigent(api_type="anthropic")
with agent.session(system="You are a professional translator.") as s:
result = s.chat("Translate to English: 你好世界")
print(result)
Tier 2 — Tools
from aigent import Aigent, tool
@tool
def get_weather(city: str, unit: str = "celsius") -> str:
"""Get current weather for a city."""
# In real code, call a weather API here
return f"{city}: 22°{unit[0].upper()}"
agent = Aigent(api_type="openai")
with agent.session(tools=[get_weather]) as s:
reply = s.chat("What's the weather in Beijing?")
print(reply)
The @tool decorator auto-generates JSON Schema from your function signature and docstring — no manual schema writing.
Tier 3 — Full Control
from aigent import Aigent, Message
agent = Aigent(api_type="anthropic")
# Manually orchestrate messages
resp = agent.raw(
[Message.system("You are helpful."), Message.user("Hi!")],
max_tokens=200,
temperature=0.7,
)
print(resp.content) # str
print(resp.usage) # Usage(prompt_tokens=..., completion_tokens=...)
Session & Role API
agent = Aigent()
with agent.session() as s:
# Chat as user (default)
s.chat("Hello")
# Chat as assistant
s.chat("I'm doing great!", role="assistant")
# Streaming
for token in s.chat("Write a poem", stream=True):
print(token, end="")
# Insert without sending — build context gradually
s.user.insert("I want to learn Python.")
s.assistant.insert("Great choice! Where would you like to start?")
reply = s.chat() # sends existing history without adding new message
# Role objects
s.system.insert("You are a Python expert.")
s.role("tool").insert('{"result": 42}', tool_call_id="call_abc")
# History management
print(s.history) # read-only list of messages
s.clear() # reset the conversation
Supported Backends
api_type |
Backend | Default Model |
|---|---|---|
"openai" |
OpenAI Chat Completions | gpt-4o |
"anthropic" |
Anthropic Messages | claude-sonnet-4-6 |
OpenAI-compatible services (Azure, local LLMs, etc.) work via api_type="openai" with a custom base_url.
Configuration
agent = Aigent(
api_type="openai",
api_key="sk-...", # or OPENAI_API_KEY env var
base_url="https://api.openai.com/v1", # or OPENAI_BASE_URL env var
model="gpt-4o", # or OPENAI_MODEL env var
system="You are helpful.", # default system prompt for all sessions
timeout=30.0,
max_retries=3,
)
Environment variables per backend:
| Env | OpenAI | Anthropic |
|---|---|---|
| API key | OPENAI_API_KEY |
ANTHROPIC_API_KEY |
| Base URL | OPENAI_BASE_URL |
ANTHROPIC_BASE_URL |
| Model | OPENAI_MODEL |
ANTHROPIC_MODEL |
Error Handling
from aigent import (
Aigent,
AuthenticationError,
RateLimitError,
APIError,
ConnectionError,
TimeoutError,
)
agent = Aigent()
try:
with agent.session() as s:
s.chat("Hello")
except AuthenticationError:
print("Check your API key.")
except RateLimitError:
print("Slow down.")
except (ConnectionError, TimeoutError):
print("Network issue.")
except APIError as e:
print(f"API returned {e.status_code}")
All exceptions inherit from LLMError.
Streaming
with agent.session() as s:
for token in s.chat("Tell me a story", stream=True):
print(token, end="", flush=True)
# Tokens are also accumulated and auto-appended to history
License
Apache 2.0 — see LICENSE.
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
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 uss_aigent-0.1.2.tar.gz.
File metadata
- Download URL: uss_aigent-0.1.2.tar.gz
- Upload date:
- Size: 32.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12b584f4a66986cc042c5bd661a3e5e5ac4666ca076401146c1ed09cc9693693
|
|
| MD5 |
d685508f3903a06a770bf8cf93ac1ec5
|
|
| BLAKE2b-256 |
57102bd1416c0dc92fb5f6693e52f7e13c306bb4ffdda49d2011040313d36f7d
|
File details
Details for the file uss_aigent-0.1.2-py3-none-any.whl.
File metadata
- Download URL: uss_aigent-0.1.2-py3-none-any.whl
- Upload date:
- Size: 25.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf7f212ad21b6793a61c93e1bf066addb7e4454dce0cff998d8703fe221eb8e7
|
|
| MD5 |
ac92492145bccbb8b0a3560326a1356e
|
|
| BLAKE2b-256 |
773653695e479eaf949035f85af5295ab2225b8a4ab9bc93887cf047b2c050d1
|