Replace the default
strands.Agent()constructor with your own factory function — all from YAML.
type:— point an agent at a custom callable instead of the built-in constructoragent_kwargs:— pass additional keyword arguments that only your factory understands- Factory receives all standard params (
name,agent_id,model,system_prompt,tools,hooks,session_manager) plus your extras
agents:
assistant:
type: ./factory.py:create_agent # your factory, not Agent()
model: default
system_prompt: You are a helpful assistant.
agent_kwargs:
greeting: "Ahoy, captain!"
personality: piratestrands-compose imports create_agent from factory.py and calls it with
all the standard agent parameters plus everything in agent_kwargs.
Your factory must return a strands.Agent instance.
⚠️ agent_kwargsis an expert feature.
strands.Agent.__init__()does not accept**kwargs— it has a fixed set of explicit parameters. strands-compose does not validateagent_kwargsat the schema level. If invalid keys reachAgent(), you get aTypeErrorat runtime.Your factory is responsible for:
- Consuming any custom keys (like
greeting,personality) before forwarding.- Filtering the remaining kwargs to only Agent-accepted parameters.
See
factory.pyin this example for the recommended pattern.
- You can also use
type:with a module path:my_package.factories:create_agent. - If a factory returns something other than
strands.Agent, strands-compose raises aTypeErrorimmediately.
pip install strands-composeuv run python examples/14_agent_factory/main.py| Prompt | What to expect |
|---|---|
What is the meaning of life? |
Response starts with "Ahoy, captain!" in pirate style |
Tell me about Python. |
Pirate personality persists across messages |
Strands agents log actions to the console through their default callback_handler.
If you want cleaner example output, set the handler to null in agent_kwargs for any agent:
agents:
my_agent:
agent_kwargs:
callback_handler: null # or ~