# Files and attachments

Source: https://agenta.ai/docs/reference/agents/files-and-attachments

Files enter an agent through session attachments and leave through file events or tool results. The sandbox working directory is separate from the attachment API.

## Upload and reference

Getting a file in front of an agent takes three calls: upload the bytes, reference the returned attachment ID from the session, then invoke the agent in that session.

Both attachment endpoints need a session ID up front. Use your own value, or run a first invoke without a `session_id` and read the minted one from the `x-ag-session-id` response header. The attachment routes accept a narrower session ID than [Invoke an agent](https://agenta.ai/docs/reference/agents/invoke-an-agent.md#request-body): 1 to 128 letters, numbers, underscores, or hyphens. A session ID containing `.` or `:` can invoke but cannot upload or reference attachments.

### 1. Upload

```
POST {AGENTA_HOST}/api/sessions/attachments?project_id={PROJECT_ID}&session_id={SESSION_ID}
```

The body is `multipart/form-data` with two fields:

| Field             | Type   | Required | Description                                                                            |
| ----------------- | ------ | -------- | -------------------------------------------------------------------------------------- |
| `file`            | file   | Yes      | The file part. Its filename and declared media type are read from the part.            |
| `idempotency_key` | string | Yes      | At most 255 characters. Reuse the same value when you retry after a network error.     |

```bash
IDEMPOTENCY_KEY=$(uuidgen)  # reuse the same value when you retry

curl -X POST \
  "https://eu.cloud.agenta.ai/api/sessions/attachments?project_id=019d952f-0000-0000-0000-000000000000&session_id=customer-case-4821" \
  -H "Authorization: ApiKey $AGENTA_API_KEY" \
  -F "file=@report.pdf" \
  -F "idempotency_key=$IDEMPOTENCY_KEY"
```

```json
{
  "count": 1,
  "attachment": {
    "attachment_id": "019d952f-0000-0000-0000-000000000010",
    "filename": "report.pdf",
    "media_type": "application/pdf",
    "size": 48213,
    "created_at": "2026-10-06T10:00:00Z"
  }
}
```

Omitting the `session_id` query parameter returns `422`. Omitting the `idempotency_key` form field returns an error naming the missing field. The request must carry a `Content-Length` header, which curl sets for you.

### 2. Reference

```
POST {AGENTA_HOST}/api/sessions/attachments/reference?project_id={PROJECT_ID}
```

```bash
curl -X POST \
  "https://eu.cloud.agenta.ai/api/sessions/attachments/reference?project_id=019d952f-0000-0000-0000-000000000000" \
  -H "Authorization: ApiKey $AGENTA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "session_id": "customer-case-4821",
    "attachment_ids": ["019d952f-0000-0000-0000-000000000010"]
  }'
```

The response echoes the referenced attachments. The request accepts at most 100 attachment IDs. The API validates project access before exposing file content to a session.

### 3. Invoke

Send the next turn in the same session. The message must carry an `attachment` content block with the attachment ID: referencing alone records the attachment for the session, and the runner fetches it only when the current user message includes its block. The full request contract is in [Invoke an agent](https://agenta.ai/docs/reference/agents/invoke-an-agent.md).

```bash
curl -X POST \
  "https://eu.cloud.agenta.ai/services/agent/v0/invoke?project_id=019d952f-0000-0000-0000-000000000000" \
  -H "Authorization: ApiKey $AGENTA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "session_id": "customer-case-4821",
    "references": {
      "workflow_revision": { "id": "019d952f-0000-0000-0000-000000000002" }
    },
    "data": {
      "inputs": {
        "messages": [
          {
            "role": "user",
            "content": [
              { "type": "text", "text": "Summarize the attached report." },
              {
                "type": "attachment",
                "attachmentId": "019d952f-0000-0000-0000-000000000010",
                "mimeType": "application/pdf",
                "filename": "report.pdf"
              }
            ]
          }
        ]
      }
    }
  }'
```

When sending messages, use image or resource blocks with a URI and MIME type, or the equivalent Vercel `file` part. See [Chat message format](https://agenta.ai/docs/reference/agents/chat-message-format.md#content-blocks).

## Place a file in the working directory

Referencing an attachment exposes it through the attachment API; it does not create a file on the sandbox filesystem, so the agent cannot `ls` or `unzip` it there. To hand the agent a file it can open with shell tools, upload it into a session mount:

```
POST {AGENTA_HOST}/api/sessions/mounts/{MOUNT_ID}/files/upload?project_id={PROJECT_ID}
```

The body is `multipart/form-data` with a single `file` field. The `MOUNT_ID` is the UUID of the session's working-directory mount, not a path. List the session's mounts and select the one named `cwd`:

```bash
curl -sS "https://eu.cloud.agenta.ai/api/sessions/mounts/?project_id=019d952f-0000-0000-0000-000000000000&session_id=customer-case-4821" \\
  -H "Authorization: ApiKey $AGENTA_API_KEY"
```

```json
{
  "count": 1,
  "mounts": [
    { "name": "cwd", "id": "019d952f-0000-0000-0000-000000000020", "session_id": "customer-case-4821" }
  ]
}
```

Use the `cwd` mount's `id` in the upload path. Uploading to a different mount's UUID places the file outside the working directory.

```bash
curl -X POST \
  "https://eu.cloud.agenta.ai/api/sessions/mounts/019d952f-0000-0000-0000-000000000020/files/upload?project_id=019d952f-0000-0000-0000-000000000000" \
  -H "Authorization: ApiKey $AGENTA_API_KEY" \
  -F "file=@bundle.zip"
```

```json
{ "path": "bundle.zip", "size": 78683 }
```

The file appears at that path in the agent's working directory on its next turn.

## Share a drive between agents

A drive is a folder of files that belongs to the project. Several agents can attach one drive. Use it for files that more than one agent reads, such as a price list, or for a folder that a team of agents writes. An agent mounts a drive only when its configuration lists it.

### 1. Create the drive

Create the drive with a slug and a name. The call needs permission to edit drives in the project. A drive created this way is shared: every project member whose role allows it can use it.

```
POST {AGENTA_HOST}/api/mounts/?project_id={PROJECT_ID}
```

```bash
curl -X POST \
  "https://eu.cloud.agenta.ai/api/mounts/?project_id=019d952f-0000-0000-0000-000000000000" \
  -H "Authorization: ApiKey $AGENTA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "mount": { "slug": "price-lists", "name": "Price lists" } }'
```

```json
{
  "count": 1,
  "mount": {
    "id": "019d952f-0000-0000-0000-000000000030",
    "slug": "price-lists",
    "name": "Price lists",
    "project_id": "019d952f-0000-0000-0000-000000000000"
  }
}
```

A slug that already exists in the project returns `409`. Slugs that start with `__ag__` are reserved and are refused. The request cannot bind the drive to an agent: a body with `agent_id` returns `422`.

To add files to the drive, upload them with `POST /api/mounts/{MOUNT_ID}/files/upload`, where `MOUNT_ID` is the `id` from the response.

### 2. Attach the drive to an agent

Add an entry to `drives` in the agent configuration and save a new revision:

```json
{
  "drives": [
    { "slug": "quote-files" },
    { "slug": "price-lists", "access": "read" }
  ],
  "home_drive": "quote-files"
}
```

Keep the agent's own drive in the list, and `home_drive` pointing to it, if the agent should keep its files and saved tools. A save that names a slug with no drive in the project returns `422` with the code `drive_not_found`. All the fields and rules are in [drives](https://agenta.ai/docs/reference/agents/agent-configuration.md#drives).

### What the agent sees

The drive appears in the agent's working folder at `drives/<slug>/`. The home drive appears at `drives/<slug>/` too, and also at `agent-files/`. `drives/README.md` lists the drives of the run with their access. The session files pane shows each drive under the same path, and the home drive under `agent-files/`.

| `access`       | What the agent can do                                                                                                    |
| -------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `"read-write"` | Read, create, change, and delete files. This is the default.                                                             |
| `"read"`       | Read files only. The folder is mounted read-only and the storage credentials allow only reads, so a write fails with a read-only file system error. |

### Several agents on one drive

Any number of agents in the project can list the same drive, each with its own access. An agent that does not list the drive does not mount it and gets no credentials for it. A private drive is mounted only in the runs of its owner; a run of anyone else does not get it. One exception: the owner's own channel installation runs the owner's personal agent with the private drives that agent lists, so people in that channel can ask about those files. See [Connect an agent to Slack](https://agenta.ai/docs/guides/connect-slack.md).

When agents write the same drive:

- A write replaces the whole file.
- When two runs write one file, the last upload wins. There are no locks and no merges.
- A run sees a change from another run after the writer's upload completes and the reader's cache refreshes. The cache refreshes at the start of each turn, and otherwise within about one minute.

## Generated files

An agent can create files in its sandbox. A streamed `file` event carries:

```json
{
  "type": "file",
  "data": {
    "url": "https://example.invalid/download/report.pdf",
    "mediaType": "application/pdf"
  }
}
```

Treat returned URLs according to their expiry and authorization rules. Download files promptly when your application needs durable ownership.

## Working-directory persistence

When `has_session_drive` is `true` (the default), the session's working folder is kept in storage and is the same folder on every turn of the session. When it is `false`, the working folder is temporary and is gone when the sandbox is removed. See [has_session_drive](https://agenta.ai/docs/reference/agents/agent-configuration.md#has_session_drive).

Files in `agent-files/` and `drives/<slug>/` persist independently of the session. They are not a substitute for application storage. Download files your application needs to own.

Do not place credentials in uploaded files or message blocks. Use project secrets, connected applications, MCP secret references, or sandbox credential bindings.
