# writeDocument

Canonical URL: https://developers.iagenify.com/sdk/ref/tools/write-document/
Language: en

`tools: { write_document: api.tools.writeDocument }`

Lets a run create or change a text file on the caller's computer — and it keeps the previous version, every time.

Status: declaration only · free · keeps the old version

There is nothing to call: `api.tools.writeDocument` is a declaration. What performs it is the run, through the machine your process is on.

## Declaring it {#declaring}

```javascript
const agent = api.agents.create({
  model: { main: session },
  storages: [pc],                       // the computer it may write to
  tools: {
    list_documents: api.tools.listDocuments,
    read_document: api.tools.readDocument,
    write_document: api.tools.writeDocument,
  },
});
```

## What the model sends {#arguments}

```javascript
{ "path": "Launch plan/brief.md", "content": "# Brief\n\nShip in March.", "mode": "create" }
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `path` | string | required | Relative to the workspace on that machine. |
| `content` | string | required | Text. Not a Buffer, not an object. |
| `mode` | string | create | `create` · `update` · `append`. |

| mode | Does | On a file already there |
| --- | --- | --- |
| `create` | Writes a new file. | Refused — it says to use `update` or `append`. |
| `update` | Replaces the content. | The old version is kept first. |
| `append` | Adds to the end. | The old version is kept first. |

## Nothing is overwritten without a copy {#versions}

Before an `update` or an `append`, the current content is written to `.versions/<path>/<timestamp>`. Hidden folders stay out of a listing, so the copies do not clutter the workspace — and the answer carries `backup`, the path of the one just made.

```javascript
// What the run reports back
{ path: "Launch plan/brief.md", mode: "update", bytes: 412,
  backup: ".versions/Launch plan/brief.md/2026-10-07T09-12-44-000Z.md" }
```

> **Why this exists at all**
>
> An agent follows text that may have come from a web page or a document. Giving it a write that cannot be undone would make one bad instruction final. Here the previous version is one read away, and you can hand it back.

## Text files only {#text-only}

The type is read from the extension, and anything that is not text is refused: `.md`, `.txt`, `.json`, `.csv`, `.html`, `.yaml`, source files. An image or a PDF is not written this way — generated media is attached to the run instead.

## A long document is written in passes {#long}

The description says so to the model: create the file, then extend it with `append`. A single enormous write competes with the answer for the turn's output budget; several passes do not.

> **It runs on the caller's computer, through the package**
>
> The server cannot reach a disk. When the agent calls this, the run asks the machine that holds the key to perform it, the package does the work there, and the answer goes back into the run. So it only works while your process is listening — and nothing of the file ever travels to be written.

## Refusals the run reports {#refusals}

- A missing `path`, or one that is not a string.
- `content` that is not text.
- A `mode` outside the three.
- `create` on a path already taken, naming the two modes that would work.
- A path outside the workspace, or an absolute one.

- [moveDocument](/sdk/ref/tools/move-document): Moving and renaming.
- [deleteDocument](/sdk/ref/tools/delete-document): Deleting, recoverably.
- [api.tools](/sdk/ref/tools): The fifteen, and the two ways to use them.

```python
agent = api.agents.create(
    model={"main": session},
    storages=[pc],                        # the computer it may write to
    tools={
        "list_documents": api.tools.list_documents,
        "read_document": api.tools.read_document,
        "write_document": api.tools.write_document,
    },
)
```

```python
{"path": "Launch plan/brief.md", "content": "# Brief\n\nShip in March.", "mode": "create"}
```

```python
# What the run reports back
{"path": "Launch plan/brief.md", "mode": "update", "bytes": 412,
 "backup": ".versions/Launch plan/brief.md/2026-10-07T09-12-44-000Z.md"}
```
