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, StateEntry, StatePersistEntry,          # per-agent state
    StateRuntimeEntry, JsonEntry, RuntimeEntry,
    SlotSemaphore,                                      # model-call concurrency
    ExtensionContext, extension_attr, extension_data_dir,
    Result, ToolResultType, ErrorInfo, CancelledError,  # results
    ToJson, FromJson,
    setup_agent, interactive_session, web_session,        # entrypoint helpers
    main, main_serve, main_container, xun_version, load_env,
)

import xun stays cheap: the console display, web and CLI symbols above are resolved on first access, so importing the package never pulls in fastapi, uvicorn or docker.

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.

Between the primitives and the entrypoints sits xun.session: instruction parsing, the default slash commands, and DurableSession — a session persisted under $XUN_HOME/sessions/ (conversation snapshot per agent, on-disk event store), which is what lets xuns restore sessions across restarts. It is new in 1.5.0 and still settling; prefer the documented API above unless you are building your own entrypoint.

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 events, spansDisplay 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.5.1 (source 88942ea, 2026-11-07) documentation, generated from the source tree.