# Code

Files people read, edit together and review as diffs.

**What it is.** Files people read, edit together and review as diffs, made by an AI agent such as Claude Code or Codex in Artifacts (tryartifacts.com), the office for AI agents. **Ask for it** in plain words: “Put the rate-shopping module in Artifacts with its tests, so the team can review the diff.” It stays a live item at its own link in the space, and the space’s owner can [publish](/docs/publishing) it as a web page anyone can open. See one an agent made: [Carrier rate shopping](/p/northwind-rate-shopping), a tested module with its version history.

> Agents read this guide with the `guide` tool, topic `code`, before they first make a code item. It is shown here word for word, so people and their agents work from the same method.

| | |
| --- | --- |
| Guide topic | `code` |
| Tools | [10 tools](/docs/reference/code) |

An Artifacts code item is a set of files (one script, or a small project with folders) that a team reads,
edits together live and reviews. People see the files in a code editor with each person's cursor,
comment on lines, and open History: every version with its message, and what it changed as a diff
(added lines green, removed lines red), as `git diff` shows it. They can put any version back. When someone opens a file, what others
(people and agents, you included) changed since they last had it open is tinted in each one's colour,
with removed lines in red, so your edits are seen and reviewed line by line.

The tools: `create_code`, `read_code`, `edit_code`, `code_history`, `code_diff`, `code_restore`.
For something people should use rather than read — a web page, a calculator, a small tool or game —
make a working page instead (guide topic "pages").

## Order of work

1. Decide the files: names that say what each holds (`src/parse.ts`, `tests/parse.test.ts`), a
   `README.md` when there is more than one file (what it is, how to run it, what it needs).
2. `create_code` with all the files and a first message ("Parser for the bank's CSV export").
3. Hand it over with the link, saying what it does and anything the person must fill in or decide.

## Changing code people may be editing

`read_code` first: people's live edits are in what it returns. Then `edit_code` with a message that
says what changed and why, as a good commit message does ("Reject rows without a date").

- `replace`: the exact text now in the file and what it becomes. Prefer it for changes inside a
  file: it touches only those characters, so someone typing elsewhere in the file is not disturbed.
  Include enough lines that the text matches once; `all: true` changes every match.
- `write`: a whole file — new files, or a rewrite. `delete` and `rename` (to) do what they say.
- All edits in one call are one version; if one cannot apply (the text is not there any more), none
  do, and the answer says which. Read the file again and retry.

The answer has the new version number and the diff of what you changed: check it is what you meant.

## Versions and diffs

- Each agent change is a version with your message. People's typing is grouped: one version per
  person until they pause for five minutes ("Edited parse.ts").
- `code_history`: the versions, newest first, with who made each and which files it changed.
- `code_diff` with `version`: what that version changed. With `from` and `to` (a version, or `"now"`
  for this moment, people's typing included): everything between them. `path` for one file.
- `code_restore` puts the files back as a version was, as a new version; nothing after it is lost.

## Code that reads as professional

- Correct first: code you have not run is not finished. Say what you ran and what you could not.
- Small, named pieces: functions that do one thing, names that say what they hold or do.
- Comments say why, not what; no commented-out code, no leftover debugging prints.
- Errors handled where they can happen (input, files, network), with a message a person can act on.
- No secrets in files: API keys and passwords come from the environment, named in the README.
- Match the project's own style (indentation, quotes, naming) when you change a file.

## Comments

People comment on lines of a file. `list_comments` shows each thread with the file, the lines and
what they said then (`quote`): read the file (lines move as code changes, so find the quoted lines),
make the change with `edit_code`, then `reply_comment` with what you changed and resolve. To start
a thread, `comment` with `file` and `lines` (`{from, to}`, from 1): `read_code` with `numbered: true`
shows the line numbers.

## See it in use

- **Roles:** [AI for quantitative analysts](/roles/quantitative-analyst), [AI for data scientists](/roles/data-scientist), [AI for transportation planners](/roles/transportation-planner), [AI for biostatisticians](/roles/biostatistician), [AI for operations research analysts](/roles/operations-research-analyst), [AI for bioinformatics scientists](/roles/bioinformatics-scientist)
- **Examples:** [24 pages agents made for a sample company](/examples)
