> ## Documentation Index
> Fetch the complete documentation index at: https://datagen.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 4. Build an agent

> Build an agent that uses the model and the knowledge base, answers questions in chat, and posts a weekly report to Slack

Now you put the analytic model from part 1 and the knowledge base from part 3 to work. You build an agent that answers business questions, then give it a weekly automation that posts a report to a Slack channel.

## Before you start

* You completed [3. Build a knowledge base](/tutorial/03-build-context).
* To post to Slack, the agent needs Slack tools: a Slack MCP server credential in the vault the agent uses. With workspace defaults, that is the **Default** vault. See [Vault](/foundation/vault) and [Tools](/agents/tools) to add it.

## 1. Create the agent

1. Click **Agents** on the left navigation rail, then **Create New**.
2. Enter an **Agent name** and a **Description**.&#x20;
3. Leave the **Runtime** fields -- **Vault**, **Tool bundle**, **Default model** and **Sandbox environment** -- at their defaults.
4. Click **Create & open builder**.

The Agent Builder opens in three columns: **Chats**, **Config** and **Versions** on the left, the builder chat in the middle, and **Sandbox files** on the right.

## 2. Connect the model and the knowledge base

Open the **Config** tab on the left.

<Steps>
  <Step title="Connect the knowledge base">
    Under **Knowledge bases**, open **Pick a context…**, select the knowledge base from part 3, and click **+**. The agent can now read it, mounted read-only in its sandbox.
  </Step>

  <Step title="Connect the analytic model">
    Under **Analytic models**, open **Pick models…**, select the model from part 1, and click **Connect model**. Only published, materialized models can be connected.
  </Step>

  <Step title="Save">
    Click **Save config**. Connections apply from the agent's next turn.
  </Step>
</Steps>

<Tip>
  An attached knowledge base is read-only by default. To let the agent write back into it -- its memory of what it learned -- open the knowledge base's **Agents** tab and set **Access** to **Read & write** or **Read & write (ask first)**. See [Use a knowledge base](/context/use) and [Memory](/context/memory).
</Tip>

## 3. Write the agent's instructions

In the builder chat, describe what the agent does. The builder writes the agent's instructions and files in the sandbox; you can see them in **Sandbox files**.

```text Example prompt theme={null}
This agent is an analyst for <team or business>.
- For numbers, always query the connected analytic model. Never estimate.
- For definitions, policies and context, read the connected knowledge base
  and cite the file you used.
- Answer in a few sentences, then show the numbers in a small table.

It also writes a weekly report: <the metrics to cover, the comparison to
make, and how long it should be>.
```

## 4. Test it

Click **Test run** in the header. The **Test run** tab runs the unpublished draft, so you can check answers before anyone else sees the agent. Try questions that need both the model and the knowledge base:

```text Example questions theme={null}
<a question answered from the model>
<a question that needs a definition or rule from the knowledge base>
```

Refine the instructions in the builder chat until the answers are right. Click **New run** to test again.

## 5. Publish

Click **Publish** in the header. In **Publish this agent**, add **Release notes (optional)** and click **Publish**. Publishing saves a version and deploys the agent: the agent is enabled and appears in **Chat**.

To ask it questions any time, click **Chat** on the navigation rail, choose the agent with **Select an agent**, and type your question.

## 6. Schedule the weekly report

<Steps>
  <Step title="Open Automations">
    Open the agent's detail page -- click **Agent** at the top left of the builder -- and go to the **Automations** tab. Click **New Automation**.
  </Step>

  <Step title="Describe the job">
    Enter a **Name**, for example `Weekly report`. In **What should it do?**, describe the report and where to post it:

    ```text What should it do? theme={null}
    Write this week's report as described in your instructions, then post it
    to the #<channel> Slack channel. Lead with the three numbers that changed
    most since last week.
    ```
  </Step>

  <Step title="Add a weekly trigger">
    Under **Triggers**, click **Add trigger** and choose **Scheduled**. Set **Frequency** to **Every week**, pick the **Day of week** and **Time**, and set **Time zone** to your team's time zone.
  </Step>

  <Step title="Save">
    Click **Create**.
  </Step>

  <Step title="Try it now">
    Click **Run now** on the automation's row instead of waiting for the schedule. Check the Slack channel for the report.&#x20;
  </Step>
</Steps>

<Note>
  The automation posts to Slack through the Slack tools in the agent's vault, because the prompt asks for it. To chat with the agent inside Slack instead -- mention it and get an answer in the thread -- give it its own Slack bot on the **Channels** tab. See [Channels](/agents/channels).
</Note>

## What you built

* A published agent that answers questions from the analytic model and the knowledge base.
* A weekly automation that writes a report and posts it to Slack.

Learn more in [Build agents](/agents/build), [Triggers](/agents/triggers), [Channels](/agents/channels) and [Versions and logs](/agents/versions-and-logs).

<CardGroup cols={2}>
  <Card title="Previous: Build a knowledge base" icon="arrow-left" href="/tutorial/03-build-context">
    Turn the documents into a versioned knowledge base.
  </Card>

  <Card title="Next: Ship an app" icon="arrow-right" href="/tutorial/05-ship-an-app">
    Build a marimo dashboard and share it with your team.
  </Card>
</CardGroup>


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