agentu
Run AI agents in Python with zero dependencies, subprocess tool isolation, AST-compiled contracts, and self-correction.
pip install agentu
Quick start
from agentu import Agent
def search_products(query: str, max_price: int = 1500) -> list[dict]:
"""Search catalog for matching products."""
return [
{"name": "ThinkPad X1 Carbon", "price": 1399},
{"name": "MacBook Air M4", "price": 1199},
]
agent = Agent("sales", model="gemini-2.5-flash").with_tools([search_products])
# Call a tool directly
raw = await agent.call("search_products", {"query": "laptop"})
# Or let the LLM run the multi-turn loop
res = await agent.infer("Find me laptops under $1500")
print(res.text)
call() invokes a tool directly. infer() runs the async multi-turn agentic loop and returns an InferResult.
Zero-config providers
# Picks up GEMINI_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, GROQ_API_KEY, DEEPSEEK_API_KEY
gemini = Agent("analyst", model="gemini-2.5-flash")
claude = Agent("reviewer", model="claude-sonnet-4-20250514")
openai = Agent("coder", model="openai/gpt-4o")
groq = Agent("fast", model="groq/llama-3.3-70b-versatile")
local = Agent("offline", model="ollama/qwen3:latest")
All four protocol drivers (GeminiDriver, ClaudeDriver, OpenAIDriver, OllamaDriver) speak native REST/SSE over Python's stdlib (asyncio + ssl) out of the box and auto-upgrade to pooled aiohttp when installed — zero vendor SDK bloat.
Sandboxed tool execution
agent = Agent("ops", model="gpt-4o").with_sandbox(
read_tools=[search_products, read_logs],
write_tools=[save_report, send_email],
timeout=10,
max_memory_mb=256,
allow_network=False,
)
read_toolsrun asREADONLY;write_toolsrun asWRITE;DANGEROUStools stay blocked unless enabled via.with_permissions(allow_dangerous=True)- Every sandboxed tool runs in an isolated subprocess — hung tools are killed at
timeoutwithout crashing your process - Defer large tool catalogs with
.with_tools([core_tool], defer=[...100_extra_tools])—search_toolsranks them via Fielded Okapi BM25F + character 3-gram cosine (96.7%Recall@1 /100%Recall@3 in107 µsacross 50 tools vs53.3%naive word overlap)
Code Mode
agent = Agent("bot", model="claude-sonnet-4-20250514", codemode=True).with_tools([
search_products, check_inventory, save_report
])
res = await agent.infer("Find in-stock laptops and save a summary")
Instead of N sequential JSON tool round-trips, the LLM writes typed Python against tools.* in a restricted sandbox (math, json, re allowed; os, sys, subprocess, socket blocked) and self-corrects on runtime errors.
Structured outputs + tools
from agentu import Agent, InferResult
class DealSummary:
top_pick: str
price: int
rationale: str
def check(self):
assert 0 < self.price <= 1500
assert len(self.rationale) >= 12
res: InferResult[DealSummary] = await agent.infer(
"Pick the best laptop deal",
output_type=DealSummary,
)
print(res.data.top_pick, res.data.price)
# Or pass an inline dict shape with zero classes
quick = await agent.infer("Best budget pick", output_type={"pick": str, "price": int})
print(quick.data.pick)
Plain classes need zero imports: agentu lifts check() assertions via ast into JSON Schema (minimum, maximum, minLength, enum, pattern) before the first LLM call, heals malformed/truncated LLM JSON in a single O(n) pushdown pass (30.5 µs p50), and reports all violated assertions with live field values on retry. @dataclass, TypedDict, and Pydantic BaseModel work via duck-typing too.
Guardrails & hooks
from agentu import Agent, PII, ContentFilter, HookAction, HookResult
async def block_prod_deletes(tool_name, params, ctx):
if tool_name == "drop_table":
return HookResult(action=HookAction.DENY, reason="Production drops blocked")
return HookResult(action=HookAction.ALLOW)
agent = (
Agent("support", model="gpt-4o")
.with_guardrails(output_guardrails=[PII(), ContentFilter()], max_corrections=2)
.with_hooks(pre_tool=block_prod_deletes)
)
Guardrail violations feed back into the model for automatic self-correction up to max_corrections attempts. Hooks intercept pre_tool, post_tool, and on_stop with ALLOW, DENY, or MODIFY.
Multimodal & image generation
res = await agent.infer(
"Cross-check the keynote demo against the architecture PDF:",
media=[
{"type": "video", "url": "https://youtu.be/7Z5Vy9JBANs", "processing": "agentic"},
"./latency_chart.png",
"./whitepaper.pdf",
],
)
image = await agent.generate_image(
"Minimalist isometric server rack on warm paper",
model="gpt-image-2",
output_path="renders/rack.png",
)
When targeting text-only models (e.g. deepseek-chat or local Ollama), install pip install "agentu[media]" and agentu automatically degrades videos and documents into Markdown transcripts.
Skills, rules & MCP
agent = await (
Agent("engineer", model="claude-sonnet-4-20250514", auto_discover_rules=True)
.with_skills(["./skills/security-audit", "github:hemanth/skills/python-perf"])
.with_mcp(["http://localhost:3000", {"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."]}])
)
Auto-discovers AGENTS.md / CLAUDE.md from your repo root, loads SKILL.md progressive instructions only when relevant, and connects to HTTP, Streamable HTTP, or STDIO MCP servers.
Memory, context & caching
agent = (
Agent("researcher", model="gemini-2.5-flash", cache=True)
.with_context(max_tokens=100_000, compaction="auto", keep_recent=5)
)
agent.remember("Customer prefers EU regions and PostgreSQL 17", importance=0.9)
Short-term working memory + SQLite long-term recall come built in. Context compaction triggers at 80% token budget (truncate old tool outputs → summarize → keep recent turns).
Workflows, sub-agents & Ralph
from agentu import Agent, ralph, run_maker_checker
pipeline = (researcher("Gather benchmarks") & analyst("Check pricing")) >> writer("Draft memo")
out = await pipeline.run()
reviewed = await run_maker_checker(maker=coder, checker=reviewer, task="Write rate limiter", max_rounds=3)
loop_stats = await ralph(coder.with_worktree(), "PROMPT.md", max_iterations=25, timeout_minutes=20)
Compose steps with >> (sequential) and & (parallel), delegate across agents with .with_agents([...]), isolate git edits with .with_worktree(), or run autonomous ralph loops with checkpoint recovery.
Observability, evals & CLI
# One-shot prompt from terminal
agentu run "Summarize recent git commits" --model gemini-2.5-flash
# Interactive dev REPL (preserves live HTTP & MCP sessions across turns)
agentu dev my_agent.py
# Autonomous Ralph loop or FastAPI + SSE + Dashboard server (pip install "agentu[serve]")
agentu ralph PROMPT.md --max 30
agentu serve my_agent.py --port 8000
Attach OpenTelemetry GenAI spans via .with_otel() (agentu[otel]), score datasets and tool trajectories with await evaluate(agent, cases), or serve HTTP/SSE/WebSocket endpoints with serve(agent).
Extras
pip install "agentu[http]"— Progressiveaiohttpconnection-pool accelerationpip install "agentu[serve]"— FastAPI HTTP/SSE/WebSocket server & live dashboardpip install "agentu[media]"— Automatic video/PDF-to-Markdown degradationpip install "agentu[search]"— Built-inSearchAgent& DuckDuckGosearch_toolpip install "agentu[semantic]"/"agentu[redis]"/"agentu[vectors]"/"agentu[otel]"/"agentu[all]"
Related
- Interactive Codelab — interactive Python playground in your browser
License
MIT © Hemanth.HM
Metadata
Release files for agentu 2.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agentu-2.11.0.tar.gz | 311.3 kB | Without provenance | Details for agentu-2.11.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agentu-2.11.0-py3-none-any.whl | Python 3 | none | any | Details for agentu-2.11.0-py3-none-any.whl |
Total release size: 539.6 kB
Release files / agentu-2.11.0.tar.gz
| Download URL | agentu-2.11.0.tar.gz |
|---|---|
| Size | 311.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2152df608938e0ecab2f52aeea6bd7a010595649985967db4ad09d18ab2975e
|
|
BLAKE2b-256 checksum How to use checksums |
a58d35113a673916d0b5aa071930f771e456f513d20f19803fd2326b0d21f9ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / agentu-2.11.0-py3-none-any.whl
| Download URL | agentu-2.11.0-py3-none-any.whl |
|---|---|
| Size | 228.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23192945f1e5ae7c2e2bd0a04e8a71693d1a3126c17fca3c2aed12f04ee11a52
|
|
BLAKE2b-256 checksum How to use checksums |
3bd9cf26fdbd295937672e7d633a9969dfd809b924a68f36202da45c610016aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for agentu 2.11.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.