WorkflowFactory produces a fresh Workflow for each request. Register it in AgentOS(workflows=[...]) alongside any prototype workflows.
basic_workflow_factory.py
Step Shape That Varies by Tier
The step graph can change per request. A common pattern: add or drop steps based on a tier read fromctx.trusted.claims.
tier claim is read from verified middleware, so the tier cannot be changed by the request body. See Authorization From Verified Context for the trust model and JWT Role Factory for an end-to-end JWT example.
Async Factories
Use an async callable when you need to fetch context from a database, an HTTP service, or any other awaitable resource.async_workflow_factory.py
With an Input Schema
Declare a Pydantic model on the factory to validate client-suppliedfactory_input before the factory runs.
workflow_input_schema.py
Error Handling
RaiseFactoryPermissionError from inside the factory to reject unauthorized callers with HTTP 403. AgentOS raises FactoryValidationError (400) automatically when factory_input fails input_schema validation.
Factory Re-invocation on Read Endpoints
GET /workflows/{id}/runs/{run_id} and GET /workflows/{id}/runs re-invoke the factory because the workflow has to be reconstructed to read its session state. Pass factory_input as a query parameter to drive the rebuild: