Get an API key
api.tools

writeDocument

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.

declaration onlyfreekeeps the old version
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

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

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,
  },
});
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,
    },
)

What the model sends

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

Relative to the workspace on that machine.

contentstringrequired

Text. Not a Buffer, not an object.

modestringcreate

create · update · append.

modeDoesOn a file already there
createWrites a new file.Refused — it says to use update or append.
updateReplaces the content.The old version is kept first.
appendAdds to the end.The old version is kept first.

Nothing is overwritten without a copy

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.

// 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" }
# 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

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

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

  • 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.