---
title: "Agent lesson 2: An agent of its own"
description: "Start an agent with a key of its own and the limits you choose, see the forge hold it to them, and stop it, with every agent it started."
---

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

# Agent lesson 2: An agent of its own

**Time:** 25 minutes. **Track:** [agent builder](https://docs.forge.workreef.dev/learn/#agent-builder). **Before this:** [agent lesson 1](https://docs.forge.workreef.dev/learn/agents/drive-the-forge/).

By the end of this lesson you will be able to:

- make an agent's key, and start the agent with it, limited to the folders and roles you choose;
- see the forge hold the agent to those limits, and to what it may pass on to agents it starts;
- stop an agent, and every agent it started with it.

## An agent is born

In lesson 1 the agent acted as you: `af mcp` ran with your sign-in, and everything it did was yours. An agent of its own has its own identity. It is **born** from the person or agent that starts it, its parent, and it never has more than its parent: no more roles, no more folders, no more of the organization's keys, and no longer a life. Its line always ends at a person, who answers for it. See [People and agents](https://docs.forge.workreef.dev/concepts/agents/).

Start a forge, and write the client from lesson 1 again, since this lesson's home directory is new:

```console server
$ af-server --dev
...
```

```console check
$ source ~/af-dev/dev/env
$ cat > ~/agent-session <<'END'
> #!/bin/bash
> # A minimal MCP client. It runs `af ... mcp DIR`, the options after DIR going to af, and makes
> # the session's calls from stdin one at a time, as an agent does: `NAME ARGUMENTS` calls a tool
> # and prints its answer; `! COMMAND` runs a command between calls.
> coproc MCP { af "${@:2}" mcp "$1"; }
> say() { printf '%s\n' "$1" >&"${MCP[1]}"; }
> say '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"agent-session","version":"1"}}}'
> read -r _ <&"${MCP[0]}"
> say '{"jsonrpc":"2.0","method":"notifications/initialized"}'
> id=0
> while IFS= read -r step; do
>   case "$step" in
>     '! '*) eval "${step#! }" ;;
>     ?*) id=$((id + 1)); name=${step%% *}
>         say "{\"jsonrpc\":\"2.0\",\"id\":$id,\"method\":\"tools/call\",\"params\":{\"name\":\"$name\",\"arguments\":${step#* }}}"
>         read -r reply <&"${MCP[0]}"
>         case "$reply" in
>           *'"structuredContent":'*) reply=${reply#*\"structuredContent\":}; reply=${reply%,\"isError\":*} ;;
>           *'"error":'*) reply=${reply#*\"error\":}; reply=${reply%\}} ;;
>         esac
>         printf '%s -> %s\n' "$name" "$reply" ;;
>   esac
> done
> END
$ chmod +x ~/agent-session
```

Land a readme of your own first, so main has a file outside the folder the agent will be given:

```console check
$ af workspace add ~/mine
Workspace at /home/you/mine
Paths: **
main has no landing yet
$ cd ~/mine
$ af new -m "Add a readme"
Working on change xywtvwwopvknrnwlwlllnnqvtulvvwzt at commit f81274cee3ecbccd6a9f998ab94cfbbfe79adc6bc692173e10d619fecea70772
$ printf 'Atoll Forge\n' > README
$ af submit
Submitted commit 791e4a615e70c28e09632848871c53b883bc77d4975045be11e95b4bf4c03007, 1 edit in its last snapshot
```

```console eventually
$ af status
Change xywtvwwopvknrnwlwlllnnqvtulvvwzt (landed) at commit 791e4a615e70c28e09632848871c53b883bc77d4975045be11e95b4bf4c03007
```

## Its key

An agent makes its own key, and the private half never leaves it. `af key` writes a new key to the credential file and prints its public half, which is all the parent is given:

```console check
$ af --credential ~/helper.key key
a4664907f00d069c746c4236469a27af31e20e243ba2c12ccc47a8e7a3b4c127
```

`--public` prints it again later, making none; a second `af key` on the same file is refused, so a key is never overwritten:

```console check
$ af --credential ~/helper.key key --public
a4664907f00d069c746c4236469a27af31e20e243ba2c12ccc47a8e7a3b4c127
$ af --credential ~/helper.key key
Error: /home/you/helper.key exists; a key is made once, in a file of its own (`af key --public` prints its public key)
[1]
```

## Start it

You start the agent from your own session with `start_agent`, giving its public key, as `af key --public` prints it, what it is, and its limits. This one may read and write only in `docs`:

```console check
$ ~/agent-session ~/me <<END | tee ~/helper.birth
> start_agent {"public_key":"$(af --credential ~/helper.key key --public)","program":"docs-helper","version":"1.0","model":"example-model","instructions":"Keep the docs current.","purpose":"keeps the docs current","folders":["docs"]}
> END
start_agent -> {"agent":"c570c8ad885bd56bb5f9d30df756fa664e5d95d108ccf3292aba20447bf68e9e","expires_ms":1795680106019}
```

The forge records its birth: who started it, the program, version and model it reports, what it is for, and its instructions as their SHA-256, so the record proves which instructions it had without holding them. Its roles are reader and writer, since none were named. `expires_ms` is when it stops on its own: no later than its parent, and no more than a day from now, the longest this forge lets an agent live.

The agent's ID and its key are its credential. The lab keeps the ID in `AGENT`, where you would copy it:

```console check
$ export AGENT=$(sed -n 's/.*"agent":"\([0-9a-f]*\)".*/\1/p' ~/helper.birth)
```

## The agent at work

The agent runs `af mcp` with its own key and ID, and does what lesson 1's agent did, as itself:

```console check
$ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<'END'
> open_workspace {"patterns":["**"]}
> start_change {"description":"Start the docs","author_name":"docs-helper","author_email":"helper@example.com"}
> ! mkdir -p ~/helper/files/docs && printf '# Docs\n' > ~/helper/files/docs/index.md
> submit {}
> wait {}
> END
open_workspace -> {"directory":"/home/you/helper/files","landing":1,"patterns":["**"],"written":0}
start_change -> {"change":"okwnyomywpxxpmptmkvopwxwwosznoum","commit":"b643a7c83bc31b04c475bb70db94c00e99b1be7640b51bc5ab29a4db6dae69d6"}
submit -> {"commit":"484c61cddaba40b9eaee2d7a22584ed221cd3d0f391b0f89d93d47e44767c9de","edits":1}
wait -> {"commit":"484c61cddaba40b9eaee2d7a22584ed221cd3d0f391b0f89d93d47e44767c9de","detail":"the change is on main","event":"landed"}
```

## Held to its folders

The agent may write only in `docs`. A file it writes anywhere else is left out of its snapshots, as jj leaves out a file it refuses: the file stays on disk, untracked, and its contents are never sent. Each call that snapshots warns of it, and `status` lists it under `refused`; the agent deletes it and abandons the change:

```console check
$ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<'END'
> open_workspace {"patterns":["**"]}
> start_change {"description":"Edit the readme","author_name":"docs-helper","author_email":"helper@example.com"}
> ! printf 'Atoll Forge\n' > ~/helper/files/README
> diff {}
> status {}
> ! rm ~/helper/files/README
> abandon {}
> END
open_workspace -> {"directory":"/home/you/helper/files","landing":2,"patterns":["**"],"written":0}
start_change -> {"change":"oqmtsuoqlzsnptxklzryvzvxkzmnwlqo","commit":"47921d8b3e95597b57d8056d6bdef6fa45bd23720ec12dc4c9493f685143d861"}
Warning: Refused to snapshot some files:
  README: this actor may not write it
diff -> {"base":"484c61cddaba40b9eaee2d7a22584ed221cd3d0f391b0f89d93d47e44767c9de","commit":"47921d8b3e95597b57d8056d6bdef6fa45bd23720ec12dc4c9493f685143d861","files":[]}
Warning: Refused to snapshot some files:
  README: this actor may not write it
status -> {"change":"oqmtsuoqlzsnptxklzryvzvxkzmnwlqo","commit":"47921d8b3e95597b57d8056d6bdef6fa45bd23720ec12dc4c9493f685143d861","conflicts":[],"findings":0,"findings_partial":false,"landing":2,"refused":["README"],"state":"open","waiting_for":["scan"],"withheld":[]}
abandon -> {"change":"oqmtsuoqlzsnptxklzryvzvxkzmnwlqo"}
```

## What it may pass on

An agent can start agents of its own, for parts of its task, but never give them more than it has. Make two keys, and have the agent ask for an approver, which no agent may be, and then for a reader:

```console check
$ af --credential ~/approver.key key
a98aba27cb995682d3c819d732c7a43e6085b56c71100e0ee8187c95909f04bf
$ af --credential ~/reader.key key
022919971e6184f0d21f71b5481d2d7f5fc2b4ab7274f75c96e732e37c3c0644
$ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<END | tee ~/reader.birth
> start_agent {"public_key":"$(af --credential ~/approver.key key --public)","program":"approver","version":"1.0","model":"example-model","instructions":"Approve changes.","purpose":"approves changes","roles":["approver"]}
> start_agent {"public_key":"$(af --credential ~/reader.key key --public)","program":"docs-reader","version":"1.0","model":"example-model","instructions":"Read the docs.","purpose":"reads the docs","roles":["reader"]}
> END
start_agent -> {"code":-32603,"message":"PermissionDenied: an agent is a reader or a writer, no more"}
start_agent -> {"agent":"8b66fcead5539087ed4415ba98a63d40b394439cd16095710383e55e26b28093","expires_ms":1795680106019}
$ export READER=$(sed -n 's/.*"agent":"\([0-9a-f]*\)".*/\1/p' ~/reader.birth)
```

The reader reads in `docs`, and nothing more: it was given no folders, so it has its parent's, and no role but reader. Its workspace gets only the file in `docs`, and to it the readme is not there:

```console check
$ ~/agent-session ~/reader --credential ~/reader.key --actor $READER <<'END'
> open_workspace {"patterns":["**"]}
> read {"path":"docs/index.md"}
> read {"path":"README"}
> start_change {"description":"Try to write","author_name":"docs-reader","author_email":"reader@example.com"}
> END
open_workspace -> {"directory":"/home/you/reader/files","landing":2,"patterns":["**"],"written":1}
read -> {"commit":"484c61cddaba40b9eaee2d7a22584ed221cd3d0f391b0f89d93d47e44767c9de","kind":"file","path":"docs/index.md","size":7,"text":"# Docs\n"}
read -> {"code":-32602,"message":"README is no file at 484c61cddaba40b9eaee2d7a22584ed221cd3d0f391b0f89d93d47e44767c9de"}
start_change -> {"code":-32603,"message":"PermissionDenied: 8b66fcead5539087ed4415ba98a63d40b394439cd16095710383e55e26b28093 holds [\"reader\"], and this needs [\"writer\"]"}
```

## Stop it

You may stop any agent you started, and an administrator any agent at all. Stopping an agent stops its whole line, every agent it started and theirs, at once:

```console check
$ af stop $AGENT --reason "its task is done"
stop reference 6d1965658bfb1f3b19625e32119f41efab41804ed6c2ff2a68a21ea46effed44
stopped c570c8ad885bd56bb5f9d30df756fa664e5d95d108ccf3292aba20447bf68e9e
stopped 8b66fcead5539087ed4415ba98a63d40b394439cd16095710383e55e26b28093
```

From then on the forge refuses it, and every agent below it:

```console check
$ af --credential ~/helper.key --actor $AGENT status
Error: Unauthenticated: c570c8ad885bd56bb5f9d30df756fa664e5d95d108ccf3292aba20447bf68e9e is not a live actor
[1]
$ af --credential ~/reader.key --actor $READER status
Error: Unauthenticated: 8b66fcead5539087ed4415ba98a63d40b394439cd16095710383e55e26b28093 is not a live actor
[1]
```

## Exercises

1. Start an agent of your own with `"roles":["approver"]`. What does the forge say?

   ```console check
   $ af --credential ~/third.key key > ~/third.pub
   $ ~/agent-session ~/me <<END
   > start_agent {"public_key":"$(cat ~/third.pub)","program":"third","version":"1.0","model":"example-model","instructions":"Approve.","purpose":"approves","roles":["approver"]}
   > END
   start_agent -> {"code":-32603,"message":"PermissionDenied: an agent is a reader or a writer, no more"}
   ```

2. Start it again with no roles, and stop it at once. What does `af stop` print, and why only one line?

   ```console check
   $ export THIRD=$(~/agent-session ~/me <<END | sed -n 's/.*"agent":"\([0-9a-f]*\)".*/\1/p'
   > start_agent {"public_key":"$(cat ~/third.pub)","program":"third","version":"1.0","model":"example-model","instructions":"Wait.","purpose":"waits"}
   > END
   > )
   $ af stop $THIRD --reason "not needed"
   stop reference eb8108600084d0d57fd60c4f51528c80cbf7aa306b0817fc0cef22ee9aa6570f
   stopped 0f7bd6d18bc9d5f5ce2da1a8a2e2180e0397ef031894f715eb9e3484a1599de7
   ```

## Check your understanding

<details>
<summary>Where is the agent's private key, and what does the forge hold of it?</summary>

In the agent's own credential file, which `af key` wrote. The forge holds only the public half, given at the birth, and the agent proves it holds the private half each time it connects.

</details>

<details>

<summary>

Why did one `af stop` stop two agents?

</summary>

The reader was born from the docs agent, so it was in the docs agent's line. Stopping an agent stops everything below it: nothing it started can outlive it.

</details>

<details>
<summary>The docs agent asked for a reader with no folders named. Where may the reader read?</summary>

Only in `docs`: an agent never has more than its parent, so naming fewer folders narrows its access, and naming none gives it its parent's.

</details>

<details>

<summary>

What if an agent limited to `docs` writes a file outside it?

</summary>

Its workspace leaves the file out of every snapshot, as jj leaves out a file it refuses: the file stays on disk, untracked, and its contents are never sent. `status` lists it under `refused`, and each command warns `Refused to snapshot some files`. The agent's other work goes on and lands without it. Delete the file, or have the access given, and it is named no more.

</details>

<details>

<summary>

What if the docs agent asks for a reader in `src`?

</summary>

The forge refuses the birth: `folder "src" is outside the folders its parent is limited to`. A sub-agent's folders must each lie inside its parent's, such as `docs/api` under `docs`, and its keys must be among its parent's, just as its roles may be no more than its parent's.

</details>

## Next

[Agent lesson 3: Narrow, fold and compare](https://docs.forge.workreef.dev/learn/agents/narrow-fold-compare/) keeps an agent's workspace to its task, folds its fixes where they belong, and compares two operations. [People and agents](https://docs.forge.workreef.dev/concepts/agents/) explains births and lines in depth.

Source: https://docs.forge.workreef.dev/learn/agents/an-agent-of-its-own/index.mdx
