Skip to content

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 Err

Tool 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 ​

TopicPage
Build an agent, dependencies, run itAgent Initialization
Config fields, ${XUN_*} placeholdersConfiguration
Uninit / Init / Final, with blocksLifecycle & Type State
Custom displays, display eventsDisplay Extension
Multi-session web serviceWeb Display Mounting
Pydantic schema outputStructured Output
Tool schema, attributes, injected contextTools
Sub-agent tools and gettersSub-agents
Hook points and takeoverHooks
Cancelling runs and childrenChained Cancellation

Xun 1.4.2dev (unreleased; source 51d23f3, 2026-10-16) documentation, generated from the source tree.