English
API Overview
Everything the entrypoints do, you can do from Python. The public surface is small — these names are re-exported from xun:
python
from xun import (
Agent, AgentConfig, Workspace, # agent and its configuration
ToolBox, ToolCallContext, tool_attr, # tools
AgentGetterParam, AgentGetterProtocol, # sub-agent spawning
DisplayAbstract, Display, NullDisplay, # displays
WebDisplay, WebDisplayService,
Hooks, HookArgs, # lifecycle hooks
Command, CommandRegistry, # slash commands
CompactorAbstract, AutoCompactor, # context budget
AgentState, JsonEntry, RuntimeEntry, # per-agent state
ExtensionContext, extension_attr, extension_data_dir,
Result, ErrorInfo, CancelledError, ToJson, FromJson,
setup_agent, interactive_session, web_session, xun_version,
)Layers
Start at the primitives — they are stable and small. Composition features (sub-agent tools, slash commands, extensions) are themselves written against the primitives, so whenever one of them does not fit, replace it with your own function.
Two conventions
Everything is synchronous. execute() returns when the run is over. Background work (sub-agents, long commands) is threaded internally and surfaced through explicit wait/list calls rather than async.
Expected failures come back as Result.
python
result = agent.instruct("…").execute() # Result[str, ErrorInfo]
if result.is_err():
print(result.unwrap_err().error) # ErrorInfo: error, details
answer = result.unwrap() # raises on ErrTool functions do not need to catch their own exceptions: a raising tool is converted into an Err result that the model sees and can react to. CancelledError is the exception — it propagates instead of being reported, see Chained Cancellation.
API or extension?
Use the API when your own script is the entrypoint: you construct the Agent, choose its tools and display, and decide when it runs. Write an extension when xun's own entrypoints should gain the capability: it receives an uninitialized agent and is otherwise built from this same API. So if you are writing your own entrypoint, use the API directly; if you want the existing entrypoints to gain a capability, write an extension. The Extension Development guide covers the second path.
Where each topic lives
| Topic | Page |
|---|---|
| Build an agent, dependencies, run it | Agent Initialization |
Config fields, ${XUN_*} placeholders | Configuration |
Uninit / Init / Final, with blocks | Lifecycle & Type State |
| Custom displays, display events | Display Extension |
| Multi-session web service | Web Display Mounting |
| Pydantic schema output | Structured Output |
| Tool schema, attributes, injected context | Tools |
| Sub-agent tools and getters | Sub-agents |
| Hook points and takeover | Hooks |
| Cancelling runs and children | Chained Cancellation |