Write rules for your agent
Updated
A contract is the file of rules Tego holds your agent to, usually agent.yaml, that says what your agent may write, read, run and connect to, and which actions wait for your answer. On this page you start from the strict setup, learn the keys you will edit, and check the file before a run.
On this page
The strict setup
You can't know in advance how an attack will reach your agent, so start with one general setup that closes the common ways out. Run this in your project folder:
tego init --preset no-secrets,no-prod,repo-only-writes --write src --write tests --command claudeIt writes agent.yaml with this line, which builds on all three presets (rule sets that ship inside Tego):
extends: [tego/no-secrets, tego/no-prod, tego/repo-only-writes]tego/no-secrets: the agent can't read your secret files (SSH keys, cloud credentials, token stores,.envfiles), and your secret environment variables never reach it.tego/no-prod: the agent reaches no network host except its model and the hosts you list.tego/repo-only-writes: the agent writes only in the folders you list, here./src/and./tests/.
Before it writes anything, tego init checks the contract and exits 1 if it does not compile. tego init --list prints the presets and the host groups you can pass to --allow-host, such as @npm and @github.
tego init adds nothing for the agent itself and prints a note for each gap. Claude Code also needs its model host and its own folder, so for Claude Code add two flags (and --force if you already ran the line above):
tego init --preset no-secrets,no-prod,repo-only-writes --write src --write tests \
--write ~/.claude --allow-host api.anthropic.com --command claudeThen add ANTHROPIC_API_KEY under env.pass (the file shows where). The dashboard's New contract page adds these needs for Claude Code and Codex by itself, and Run your agent has ready contracts for both.
Narrow it after one normal run
Run your agent once on a normal task, then let Tego propose rules that allow what that run did and nothing more:
tego status --all
tego learn <run> --out learned.yamlThe proposal lists the folders the run wrote under the project, the programs it started and the addresses it connected to. It never replaces an existing file, so copy the entries you want into agent.yaml and keep the extends line. Read it first: it allows whatever the run did, mistakes included, and it leaves environment variables and reads to your presets. Its net.allow entries are addresses; write the host name instead, for example api.example.com:443.
The keys you will use
Your entries add to the presets' entries. The repository's example contracts show complete files. The format is at schema 0 and may change before v0.2.0.
How do I limit where the agent writes?
fs:
write: ["./src/", "./tests/"]A folder covers everything beneath it. A write is changing a file's contents or making, removing, renaming or linking a file or folder. Every other write is refused. Beneath each write folder, Tego still refuses git's hooks and config and shell startup files such as .bashrc. /tmp is refused too, so point TMPDIR at one of your folders.
How do I keep an agent away from my secrets?
fs:
deny_read: ["~/.bash_history", "~/.bashrc"] # each entry must exist
env:
pass: ["ANTHROPIC_API_KEY"]Every contract refuses reads of Tego's built-in list of secret files. deny_read adds your own paths to it. no-secrets passes the agent only plain variables such as PATH, HOME and LANG; env.pass names each extra variable it needs. A token exported in your shell's startup files comes back in any shell the agent starts, so deny those files or keep tokens out of them. deny_read: [] turns the built-in list off.
How do I limit which programs run?
exec:
allow: ["bash", "git"]Anything else the agent starts is refused and recorded. An allowed program can do anything inside its own process: allowing python3 decides that Python starts, not what it does.
How do I limit network hosts?
models:
- {provider: anthropic, host: api.anthropic.com, port: 443}
net:
allow: ["registry.npmjs.org:443"]net.allow names the hosts the agent may reach. A model under models: is reached through Tego's forwarder, which records each model call and counts its tokens; a model host in net.allow is reached directly. Under no-prod, declare the model one way or the other, or its connection is refused like any other host. Each allowed host can still receive whatever the agent sends it.
How do I approve an action before it happens?
fs:
write: ["./src/"]
ask: ["./held/"]
ask:
timeout: 5mAn ask rule holds a write or a connection until a person answers, from a second terminal or the dashboard. Nobody answering in time is a refusal.
tego ask
tego approve <ask_id>
tego deny <ask_id>Keep an ask folder outside every fs.write folder: a write grant decides first, and nobody is asked.
How do I cap time and spending?
limits:
stall_after: 1h
tokens: 2000000
memory: 4GiBA run that passes its wall time, stall_after idle time, repeats, tokens or cost_usd limit ends with exit code 124. memory and pids end the run with exit 137, and cpu only slows it. cost_usd needs a price table on the machine. For an interactive agent use stall_after and no wall, since your typing counts as activity.
How do I check a contract before running it?
tego compile agent.yamlIt prints the rule table, what is allowed, refused and held, and warns about anything likely to trip the run. Nothing runs. --merged prints your contract with every preset laid in, the one contract the run enforces. --test <table.yaml> checks made-up actions against the contract.
Where to keep the contract
Keep it outside the project the agent works in, for example in ~/contracts/: Tego refuses a contract the agent could rewrite. Paths in it are relative to the folder you start tego run from, so start it from the project:
cd ~/project && tego run ~/contracts/agent.yamlWhat no contract stops is on Security and limits. For a real case taken apart rule by rule, read git reset --hard at session start.
Source: docs/user/presets.md.