Skip to main content
An AgentFactory is a registered callable that produces a fresh Agent for each request. Register it in AgentOS(agents=[...]) alongside any prototype agents.
basic_factory.py
Run it against the factory like any other agent:
See the Factories reference for the full constructor signature and parameter list.

Async Factories

Use an async callable when you need to fetch context from a database, an HTTP service, or any other awaitable resource.
AgentOS detects the coroutine and awaits it on every request.

With an Input Schema

Declare a Pydantic model on the factory to validate client-supplied factory_input before the factory runs.
input_schema_factory.py
Send factory_input as a JSON string in the run request:
If factory_input does not validate, AgentOS returns 400 before the factory runs. If factory_input is omitted entirely, AgentOS validates {} against the schema: the request succeeds when every field has a default (like ResearchInput above), and 400s otherwise. Without an input_schema, omitted factory_input leaves ctx.input as None.

Authorization From Verified Context

Use ctx.trusted.claims and ctx.trusted.scopes for any decision that affects authorization. Trusted fields are populated by middleware that has verified the request, never by client input.
The trust split keeps authorization decisions visible at code review time. See RequestContext fields for the full schema and JWT Role Factory for an end-to-end example.

Error Handling

Raise FactoryPermissionError from inside the factory to reject unauthorized callers with HTTP 403. AgentOS raises FactoryValidationError (400) automatically when factory_input fails input_schema validation.
See the Factories reference for the full exception hierarchy and the post-resolve behavior.

Developer Resources