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

# Serve and embed

> Put the data agent behind an API so a dashboard, a Slack channel, or a product widget can ask it questions.

A data agent that only runs in a notebook helps one analyst. Behind an API, it answers questions from a Slack channel, a BI dashboard's natural-language box, or an "explain this metric" widget in your product. `AgentOS` turns the agent into that API.

Start [PgVector](/knowledge/vector-stores/pgvector/overview) on port 5532 before running this example.

```python data_agent.py theme={null}
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIResponses
from agno.os import AgentOS
from agno.tools.sql import SQLTools

agent = Agent(
    id="data-agent",
    model=OpenAIResponses(id="gpt-5.5"),
    db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
    tools=[SQLTools(db_url="postgresql+psycopg://readonly@warehouse/analytics")],
    learning=True,
)

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

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

Run it with `python data_agent.py`. The `app="data_agent:app"` import string has to match the module name. Every surface then calls the same endpoint:

```bash theme={null}
curl -X POST http://localhost:7777/agents/data-agent/runs \
  -F 'message=MRR by plan, last 6 months' \
  -F 'user_id=ab@acme.com' \
  -F 'session_id=q4-review' \
  -F 'stream=false'
```

## Surfaces a data agent lives on

| Surface          | Shape                                                                   |
| ---------------- | ----------------------------------------------------------------------- |
| Slack channel    | An interface maps a thread to a session; the team asks in plain English |
| Dashboard NL box | A widget posts the question and renders the answer plus its SQL         |
| Scheduled digest | A cron job runs the agent and posts "yesterday's numbers" every morning |
| Backend check    | A pipeline calls the agent to sanity-check a metric before publishing   |

The serving model is identical to a [product agent](/use-cases/product-agents/serve-as-an-api). The difference is what the agent does, not how it is served. `user_id` and `session_id` select the conversation history for a run.

<Warning>
  This example leaves AgentOS authorization disabled. Callers can supply their own user and session identifiers. Enable AgentOS authorization before network exposure, and connect `SQLTools` with a database role that enforces read-only access.
</Warning>

## Shared learnings, separate sessions

A data agent's value compounds when corrections are shared across the team. Conversation threads stay scoped by `user_id` and `session_id`. `learning=True` only enables the per-user profile and memory stores, so to share what the agent learns, pass a knowledge base to `LearningMachine` instead. That enables the [Learned Knowledge store](/learning/stores/learned-knowledge), whose namespace defaults to `"global"`, so a fix triggered by one analyst's question helps everyone.

```python theme={null}
from agno.knowledge import Knowledge
from agno.learn import LearningMachine
from agno.vectordb.pgvector import PgVector

knowledge = Knowledge(
    vector_db=PgVector(
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
        table_name="warehouse_learnings",
    ),
)

agent = Agent(
    id="data-agent",
    # same model, db, and tools as above
    learning=LearningMachine(knowledge=knowledge),
)
```

The store runs in `AGENTIC` mode: the model decides when to call `save_learning` and `search_learnings`. To keep learnings inside one team or tenant, set `namespace` on `LearningMachine`.

| State                         | Scope                                                           |
| ----------------------------- | --------------------------------------------------------------- |
| Conversation thread           | Per `user_id` and `session_id`                                  |
| Learnings about the warehouse | Learned Knowledge store, shared `"global"` namespace by default |

## Next steps

| Task                           | Guide                                              |
| ------------------------------ | -------------------------------------------------- |
| Add Slack or a browser surface | [Interfaces](/use-cases/product-agents/interfaces) |
| Lock down the endpoints        | [Security and auth](/features/security-and-auth)   |

## Developer Resources

* [Serve as an API](/features/api)
* [Product agents: serving](/use-cases/product-agents/serve-as-an-api)
