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 | Write an extension | |
|---|---|---|
| Who runs the program | you do — your script is the entrypoint | xun does; the extension is loaded inside it |
| Typical goal | a custom agent, tool, service or batch job | add behavior to sessions you do not launch (the xun/xuns CLIs, other people's agents) |
| Mechanism | constructor arguments, ToolBox, hooks, displays | setup_extension(ctx) dropped in $XUN_HOME/extensions/ |
An extension is built entirely from the same API — it just gets an uninitialized agent instead of creating one. 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 |