Skip to main content

Introduction

Agent templates let you define agents as data rather than PHP code. Instead of writing a class that constructs an AgentBuilder with hardcoded capabilities and tools, you describe the agent’s identity, instructions, tool access, and resource budget in a definition file. At runtime, a factory turns that definition into a working AgentLoop and AgentState. This separation between definition and instantiation makes it possible to manage agents through configuration files, version them alongside your prompts, and let non-developers create or adjust agents without touching PHP. It is also the foundation of the subagent system — when a parent agent spawns a child, it looks up the child’s AgentDefinition in a registry and builds a loop from it on the fly.

AgentDefinition

AgentDefinition is the core data object that describes an agent. It is a final readonly class with the following fields:

Creating Definitions in PHP

Tool Visibility Rules

The tools and toolsDeny fields work together to control which tools the agent can access:
  • tools: null (the default) — the agent inherits all tools available in its context. For subagents, this means all tools the parent has.
  • tools: new NameList('read_file', 'bash') — only these named tools are allowed. Any other tools are excluded.
  • toolsDeny: new NameList('write_file') — these tools are removed from whatever set the agent would otherwise have, whether inherited or explicitly allowed.
The deny list is applied after the allow list. If you set tools to allow read_file and write_file, and toolsDeny to deny write_file, the agent will only have access to read_file.

ExecutionBudget

The ExecutionBudget class defines resource limits for a single agent execution. All fields are optional — null means unlimited.
When an AgentDefinition declares a budget, it is translated into UseGuards during loop instantiation.

Definition Files

Agent definitions can be stored in markdown, YAML, or JSON files. Each format maps directly to the AgentDefinition fields.

Markdown Format

Markdown definitions use YAML front matter for structured fields and the document body for the system prompt. This is the most readable format for agents with long or complex system prompts.
The document body (everything after the front matter) becomes the systemPrompt field.

YAML Format

JSON Format

All three formats produce identical AgentDefinition objects when loaded.

Loading Definitions

AgentDefinitionLoader

The AgentDefinitionLoader class parses a single file into an AgentDefinition. It selects the appropriate parser based on the file extension.
Supported extensions: .md, .json, .yaml, .yml. The loader throws a RuntimeException if the file cannot be read and an InvalidArgumentException for unsupported extensions. You can also supply custom parsers by passing an array to the constructor:

AgentDefinitionRegistry

The AgentDefinitionRegistry is a named collection of agent definitions. It supports programmatic registration, file loading, directory scanning, and auto-discovery.

Programmatic Registration

Loading from Files

During directory scans, files that fail to parse are skipped rather than causing exceptions. The errors are collected and can be inspected afterward:

Auto-Discovery

The autoDiscover() method scans up to three standard locations for agent definition files:
Paths are scanned in order: userPath, packagePath, then projectPath/.claude/agents. Later registrations overwrite earlier ones with the same name, so user-level definitions take precedence over package defaults.

Querying the Registry

Instantiation Factories

Once you have an AgentDefinition, two factory classes turn it into runnable components: one for the initial AgentState, and one for the AgentLoop that executes it.

DefinitionStateFactory

Creates an AgentState pre-configured with the definition’s system prompt, metadata, and LLM config. It implements the CanInstantiateAgentState contract.
You can also pass a seed state to merge the definition’s settings onto an existing state:
The factory applies settings in this order: system prompt, metadata merge, then LLM config. Each step is skipped if the corresponding field in the definition is empty or null.

DefinitionLoopFactory

Creates a fully configured AgentLoop from a definition. This factory implements CanInstantiateAgentLoop and is used internally by SendMessage and other session actions.
The factory builds the loop by applying the definition’s fields in order:
  1. LLM config — if the definition specifies an llmConfig, a ToolCallingDriver is created with that config.
  2. Guards — if the definition declares a non-empty budget, UseGuards is applied with the budget’s limits.
  3. Capabilities — each named capability in the definition is resolved from the AgentCapabilityRegistry and applied to the builder.
  4. Tools — if the definition references named tools, they are resolved from the tool registry and added via UseTools.

Providing a Tool Registry

When the definition references tools by name (via tools or toolsDeny), you must provide a tool registry that implements CanManageTools:
If a definition references tools and no registry is provided, DefinitionLoopFactory throws an InvalidArgumentException. Unknown tool names also cause an exception, listing which tools could not be found.

Event Propagation

Pass an event handler to propagate events from instantiated loops to a parent dispatcher:

AgentCapabilityRegistry

The AgentCapabilityRegistry maps string names to capability instances. It is the bridge between definition files (which reference capabilities by name) and the PHP capability classes that implement them.
Factory-registered capabilities are instantiated on first access and cached for subsequent lookups. If the factory does not return a CanProvideAgentCapability, an InvalidArgumentException is thrown.

Opt-in Composer manifest discovery

Installed packages can expose zero-configuration capabilities and tools through their composer.json:
Discovery is deliberately explicit. Enabling it lets installed third-party packages register executable classes, so applications should opt in only for a trusted dependency set:
Discovery parses metadata and registers lazy factories; it does not instantiate contributed classes. Malformed manifest declarations are returned by $result->errors(). Missing classes, wrong interfaces, and constructors requiring arguments fail only when that specific registry entry is resolved. Root-package mappings override vendor mappings. For configured capabilities or tools, register an application factory directly instead of using the manifest.

Serializing and safely persisting definitions

AgentDefinitionSerializer renders the same canonical definition as Markdown, YAML, or JSON. Canonicalization normalizes fallback labels and empty optional values so parsing a serialized definition preserves every meaningful field.
Use FileAgentDefinitionStore for persistence policy. It accepts an existing writable root, derives the filename from a validated agent name, writes atomically, and refuses overwrite unless it is explicitly requested. It never accepts a caller-provided path. UseAgentDefinitions exposes list_agents and read_agent by default. Supply a store explicitly to add write_agent; this is the only mode that grants the agent filesystem mutation authority:
write_agent validates the complete definition before touching disk, refuses replacement by default, and refreshes the definition registry after a successful save. It does not mutate or reload an already-running agent loop.

Using with Subagents

The AgentDefinitionRegistry implements CanManageAgentDefinitions, making it the standard provider for the UseSubagents capability. When a parent agent calls spawn_subagent, the subagent system looks up the named definition in this registry and builds a child loop from it.
The subagent tool’s schema automatically includes the list of available agents and their descriptions, so the LLM knows which subagents it can delegate to. See Subagents for the full delegation model.

Serialization

AgentDefinition supports round-trip serialization via toArray() and fromArray():
This is used internally by the session persistence layer to store agent definitions alongside session state. The fromArray() method also accepts title as an alias for label to support legacy formats.