Zgent
Core Concepts

Agents

Define agent behavior with AgentDefinition and manage instances with AgentContext.

AgentDefinition

An AgentDefinition is a template that describes how to create agent instances. It specifies the system prompt, engine, tools, hooks, and routing logic.

from zgent import AgentDefinition, AgentEngine, AgentOptions, Dispatcher, DispatchResult, DispatchAction

definition = AgentDefinition(
    system_prompt="You are a code review assistant.",
    engine_type=AgentEngine.CLAUDE,
    dispatcher=Dispatcher(
        lambda msg: DispatchResult(key=msg.session_id, action=DispatchAction.ROUTE)
    ),
    options=AgentOptions(
        prefix="reviewer",
        streaming=True,
        allowed_tools=["Read", "Grep", "Glob"],
    ),
)
ParameterDescription
system_promptInstructions for the agent
engine_typeWhich execution engine to use (AgentEngine.CLAUDE)
dispatcherRoutes messages to agent instances
optionsEngine-specific options (model, tools, streaming)
toolsCustom tools the agent can call
hooksLifecycle hooks (before/after tool use, etc.)
subagentsNested agent definitions for hierarchical composition
inboundFilter or transform incoming messages
outboundFilter or transform outgoing messages

AgentOptions

AgentOptions configures engine behavior:

from zgent import AgentOptions

options = AgentOptions(
    prefix="my-agent",
    model="claude-sonnet-4-20250514",
    streaming=True,
    allowed_tools=["Read", "Write", "Bash"],
    disallowed_tools=["WebSearch"],
)

Message handlers

Inbound and outbound handlers let you filter or transform messages without modifying the agent logic:

from zgent.message import Message

def inbound_filter(msg: Message) -> Message | None:
    """Only accept messages from allowed sessions."""
    if msg.session_id.startswith("admin-"):
        return msg
    return None  # drop

definition = AgentDefinition(
    system_prompt="Admin assistant.",
    engine_type=AgentEngine.CLAUDE,
    inbound=inbound_filter,
)

SubAgents

Agents can delegate work to sub-agents for complex workflows:

from zgent import SubAgentDefinition

definition = AgentDefinition(
    system_prompt="You are a project manager. Delegate coding tasks to your sub-agent.",
    engine_type=AgentEngine.CLAUDE,
    subagents={
        "coder": SubAgentDefinition(
            description="Use for focused implementation tasks that need code changes.",
            system_prompt="You are a coding assistant.",
            model="sonnet",
        ),
    },
)

AgentContext

When a message arrives for a new session key, Zgent creates an Agent instance with its own AgentContext. The context tracks:

  • Conversation history
  • Token usage and budgets
  • Snapshot state for persistence
  • The current agent lifecycle phase

On this page