> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic Agents

> Build agents, teams, and workflows per request from JWT claims, user input, and other request-time context.

v2.6.0 (Introduced in v2.6.0)

Build a fresh Agent, Team, or Workflow for each incoming request. A factory is a callable that AgentOS invokes per request, so tools, instructions, model, and database scope can depend on who's calling.

```python tenant_factory.py theme={null}
from agno.agent import Agent, AgentFactory
from agno.db.postgres import PostgresDb
from agno.factory import RequestContext
from agno.models.openai import OpenAIResponses
from agno.os import AgentOS

db = PostgresDb(
    id="factory-demo-db",
    db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
)


def build_tenant_agent(ctx: RequestContext) -> Agent:
    user_id = ctx.user_id or "anonymous"
    return Agent(
        model=OpenAIResponses(id="gpt-5.4"),
        db=db,
        instructions=f"You are a helpful assistant for tenant {user_id}. Be concise.",
        markdown=True,
    )


tenant_factory = AgentFactory(
    id="tenant-agent",
    db=db,
    factory=build_tenant_agent,
    name="Per-tenant assistant",
    description="Builds a personalized agent per tenant on each request.",
)

agent_os = AgentOS(agents=[tenant_factory])
app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="tenant_factory:app", port=7777)
```

Hit `POST /agents/tenant-agent/runs` and the factory is invoked with a [`RequestContext`](https://agno-v2.mintlify.site/reference/agent-os/factories#requestcontext) for that request.

> [!NOTE]
>
> `id` and `db` are required on `AgentFactory`. The factory's `id` overrides any `id` set on the `Agent` returned by the callable.

## When to Use a Factory

Use a plain `Agent` / `Team` / `Workflow` when the component is shared across all callers. Reach for a factory when construction depends on the request.

| Use Case | Pattern |
| - | - |
| Tools or model vary per caller role | `AgentFactory` reading `ctx.trusted.claims` |
| Members vary per tenant | `TeamFactory` returning a `Team` with tenant-specific agents |
| Pipeline shape varies per request | `WorkflowFactory` returning a different step graph |
| Client picks persona, depth, or style | Factory with `input_schema` reading `ctx.input` |

> [!NOTE]
>
> `GET /agents/{id}`, `GET /teams/{id}`, and `GET /workflows/{id}` (the component-detail endpoints) return the factory's metadata without invoking it. See the [Factories reference](https://agno-v2.mintlify.site/reference/agent-os/factories.md) for per-endpoint behavior and discovery payload shape.

## Learn How To

> **[AgentFactory](https://agno-v2.mintlify.site/agent-os/factories/agent-factory.md)**
>
> Produce a fresh Agent per request from verified claims or client input.

> **[TeamFactory](https://agno-v2.mintlify.site/agent-os/factories/team-factory.md)**
>
> Compose a fresh Team and its members for each request.

> **[WorkflowFactory](https://agno-v2.mintlify.site/agent-os/factories/workflow-factory.md)**
>
> Compose pipelines whose step graph depends on the request.

> **[RequestContext](https://agno-v2.mintlify.site/reference/agent-os/factories.md#requestcontext)**
>
> Fields, sources, and identity precedence.

> **[JWT Middleware](https://agno-v2.mintlify.site/agent-os/middleware/jwt.md)**
>
> Populate `ctx.trusted.claims` from verified JWTs.

> **[Factories Reference](https://agno-v2.mintlify.site/reference/agent-os/factories.md)**
>
> Class signatures, parameters, and exceptions.

## Developer Resources

* [Factory examples](https://agno-v2.mintlify.site/examples/agent-os/factories/overview.md)
* [AgentFactory / TeamFactory / WorkflowFactory reference](https://agno-v2.mintlify.site/reference/agent-os/factories.md)
* [RequestContext reference](https://agno-v2.mintlify.site/reference/agent-os/factories.md#requestcontext)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.