# Session audit prompt

A general prompt for any AI agent harness (Claude Code, OpenCode, Codex, others). It works for any kind of work: code, documents, research, operations. Paste it as is. Replace `<days>` and `<sample task>` before you send it.

---

Audit my past agent sessions and make my workflow cheaper and more reliable. Give me measurable results: numbers before the change, numbers after the change, and the difference.

## 1. Read the memory

Before the setup, read what already exists. Do not change any file in this step.

- Personal memory of this harness (for example `~/.claude/projects/<folder>/memory/`, `~/.codex/`, or the memory file of the tool).
- Shared instruction files (for example `AGENTS.md`, `CLAUDE.md`, rule folders) and `LESSONS.md`.
- Existing skills, commands, agent profiles, hooks, scripts and settings files.

Classify each memory entry and each rule in one table:

| Class | Meaning | Suggested action |
|---|---|---|
| Shared | Helps other people too | Move it to a shared file |
| Personal | Only about my machine or my preferences | Keep it in personal memory |
| Setting | A limit, a model, a role or an approval step | Use it as the suggested value in the setup |
| Duplicate | The same fact in two or more places | Keep one, delete the others |
| Stale | No longer true (a missing file, an old tool, an old value) | Delete or correct it |
| Conflict | Two entries that say different things | Ask me which one is correct |

Then write suggestions for additions:

- Rules, skills, agent roles or settings that the memory shows are necessary but do not exist yet.
- Repeated corrections in the memory that a guard, a script or a rule can prevent.
- Gaps: a role or a flow that I use but that has no profile, skill or rule.

Write the table and the suggestions to `<tasks folder>/audit-<date>/memory.md`. Show me only the counts, the conflicts and the top 5 suggestions. I approve each suggestion before you apply it.

## 2. Set up the workflow with me

Before you measure or change anything, ask me for the settings below. Ask in one short round, with a suggested value for each item. When the memory has a value for an item, use it as the suggestion and show its source. Use the values from my answers, not your own.

| Setting | Suggested value |
|---|---|
| Agent roles that I need (for example: analyst, worker, reviewer, checker, small-job helper) | From the work I describe |
| Model for each role | Largest model only for design and hard work; small model for fully specified jobs |
| Reasoning effort for each role | Low for small jobs, high only for design and review |
| Maximum agents at the same time | 3 |
| Maximum agents for one task | 5 |
| Maximum fix and review rounds before the agent asks me | 2 |
| Maximum reply length of an agent | 5 lines |
| Steps where I must approve | Plan, final result, anything that is hard to undo |
| Session gates: context growth, largest call, cost | 100k tokens, 250k tokens, $10 |
| Repeated call gate | 3 times the same call with the same input in one session |
| Task folder location and gates | `<tasks folder>/<task id>/`, 50 MB or 200 files, 1 GB in total |
| What to keep after approval | The report only |
| Lesson limit and lesson areas | 10 lines; `delegation`, `review`, `testing`, `reporting`, `tools`, `approval` |

Save my answers in one settings file in the shared folder (for example `workflow.json`). All scripts, skills and agent profiles read their limits from this file. Do not write a limit in code or in a prompt. When I change a value, change only this file.

## 3. Find the data

- Find the session logs of this harness for the current folder. Examples: `~/.claude/projects/`, `~/.codex/sessions/`, the OpenCode storage folder. If you cannot find them, ask me.
- Use only the last `<days>` days.
- Tell me which logs you found, how many sessions, and the date range. Then continue.

## 4. Measure with a script, not by estimate

Write a small script that reads the logs and calculates the numbers. Do not count or estimate by reading the logs yourself. The script does the arithmetic; you only classify and explain.

Calculate these metrics for each session and in total:

| Metric | What it shows |
|---|---|
| Duration, number of calls | How long and how busy the session was |
| Input, output, cache read, cache write tokens | Where the tokens went |
| Cache hit rate | How much context was reused |
| List-price cost | Money, from a price file that I can edit |
| Context growth from the first to the last call | Context bloat |
| Largest single call | Context spikes |
| Cache rebuilds after an idle gap | Cost of pauses |
| Subagent spawns by role and model, and the model compared with the settings file | Delegation cost and wrong model choice |
| Agents at the same time, and agents for each task | Orchestration over the limits |
| Same or very similar subagent briefs | Agents created again for the same job |
| Brief size of each subagent | Agents that got too much input |
| Fix and review rounds for each task | Loops that did not stop |
| Tool calls by name, and failed calls | The most used and most failing tools |
| The same call with the same input in one session or across sessions: shell commands, file reads, searches, web fetches, connector calls | Repeated calls that a cache, a script or a saved result can replace |
| The same sequence of 3 or more calls that occurs again | Call chains that one script or one command can replace |
| Size and file count of each task folder, and its growth per session | Artifact bloat |
| A failed command followed by a different command for the same goal | Workarounds |
| User messages that repeat or correct earlier ones | Repeated flows, corrections and lesson candidates |
| Tokens of the files that load in every session, and of each skill when it loads | Context that loads when it is not needed |
| Skills that loaded without a use, and skills that were necessary but did not load | Weak trigger descriptions |

Put the prices in a separate file (for example `prices.json`), not in the code.

## 5. Apply the gates

- Use the limits from the settings file.
- Mark each session, task and task folder that is over a limit.
- If the data shows that a limit is too high or too low, suggest a new value. Do not change it until I approve.
- A session over a limit must hand off to a new session with a short written summary.

## 6. Report

Write the report to a file. Keep the chat reply short. The report includes:

1. Totals: time, tokens, cost, cache hit rate.
2. The top 5 cost causes, each with its cost.
3. Repeated flows, repeated agents, repeated calls, repeated call chains and workarounds, each with a count and a cost.
4. Orchestration: agents by role and model, wrong model choices, tasks over the agent or round limits.
5. Sessions, tasks and task folders over the gates, with the largest files.
6. Memory: the count for each class, the suggestions that I approved, and the audit findings that confirm or reject a memory entry.
7. Lessons: the lessons found in past sessions, and the open lessons in `LESSONS.md`.
8. Context loading: the always-loaded size, and the skills with weak triggers.
9. For each finding: one fix, the place for the fix, and the expected saving.

## 7. Build the orchestration as a `delegate` skill

All delegation goes through one skill, `delegate`. The main session does not spawn agents in any other way. The skill reads the settings file and does these steps:

1. **Decide if an agent is necessary.** Small work that the main session already understands stays in the main session. Delegate only work that needs its own context, a different model, or parallel time.
2. **Size the work.** Classify it as small, medium or large. The size selects the role, the model and the effort from the settings file.
3. **Reuse before you create.** If an agent of the same role already has the context for this task, send it a follow-up message. Do not create a new agent for the same job.
4. **Write a small brief.** The brief has the goal, the file paths to read, the output file to write, and the condition for done. No summaries, no copied content, no history.
5. **Check the limits.** Do not start an agent that goes over the limit for agents at the same time or agents for one task. Wait or do the work in sequence.
6. **Run in parallel only independent work.** Two agents never change the same files. An agent that needs the result of another agent waits for it.
7. **Collect one reply.** The agent replies one time, when it is done or blocked, in the reply length from the settings file. The first word is `DONE`, `NEEDS_INPUT` or `BLOCKED`. Details are in its output file.
8. **Review from the output only.** A reviewer gets only the output file. A second review uses a fresh reviewer on the new changes only.
9. **Stop loops.** After the maximum number of fix and review rounds, stop and ask me.
10. **Ask at approval steps.** At each approval step from the settings file, show me the result and wait.
11. **Log each delegation.** Write one line for each agent to the task folder: role, model, brief size, rounds, result. The audit script reads this log.

Make a fixed agent profile for each role from the settings file: a short role text, only the tools that the role needs, and its model and effort. The `delegate` skill selects from these profiles only.

## 8. Keep lessons in `LESSONS.md`

A lesson is an important decision about the way of working or the workflow. Examples: "small fixes stay in the main session", "the reviewer reads only the diff", "ask before a hard-to-undo step". A lesson is not a fact about the work itself.

**Find lessons.**
- In the audit, find them in past sessions: my corrections, my decisions at approval steps, the rules I gave more than one time, and the flows that I stopped or changed.
- In each new session, record a lesson when I make or confirm such a decision.

**Write one line for each lesson.** Use this format:

`- <date> | <area> | <decision> | <why> | <source session or task>`

- `<area>` is one word from a short fixed list in the settings file (for example `delegation`, `review`, `testing`, `reporting`, `tools`, `approval`).
- Before you add a line, use a script to look for a line with the same area and a similar decision. If one exists, update it. Do not add a copy.
- Keep `LESSONS.md` in the shared folder.

**Fix lessons at the limit.** The limit is in the settings file (suggested value: 10 lines).
- A script counts the lines after each addition. At the limit, it tells the session to stop and ask me.
- Then show me each lesson with one proposal: a rule in the instruction file, a skill step, a script, a guard hook, or a settings value. Mark a lesson that cannot be fixed as `keep`.
- I approve each proposal. Apply the approved ones, then remove those lines from `LESSONS.md`. The file keeps only open lessons.

**Load on demand, not every time.**
- Do not import `LESSONS.md` or this lessons skill in the instruction file that loads in every session. That file keeps only short rules that apply to all work.
- Give each skill a description with clear trigger words, so that the harness loads it only for a matching request. Example for the lessons skill: "record a lesson", "we decided", "from now on", "always do", "never do", "review the lessons".
- Use a hook or a script for the checks that must run every time (the line count, the repeat limit, the gates). A script costs no context until it reports.
- Split a large skill into a short main file and reference files. The main file names which reference to read for which step.
- File by topic: one file for one topic, with a name that tells the topic. An agent finds the file by its name and reads only that file.

## 9. Fix the other causes in shared files

A lesson that helps other people must be in a file that they also get, not in personal memory.

- Put rules in one instruction file that every harness reads (for example `AGENTS.md`). Other harness files only import it.
- Turn a flow that I repeat into a skill or command.
- Turn a repeated workaround into a script or a guard hook.
- Turn a repeated call into a saved result that later steps read from the task folder, or into a guard that warns at the repeat limit.
- Turn a repeated call chain into one script or command.
- Change an existing file before you add a new one.
- Personal memory keeps only facts about my machine and my own preferences.

## 10. Use these tactics

- **Measure first.** Record the numbers before a change and after it.
- **Script for facts, model for judgement.** Counting, prices and checks are in code, so that the same input gives the same result.
- **Settings in one file.** Limits, models and approval steps are in the settings file, not in code or prompts.
- **Small inputs.** Give an agent file paths, not summaries or copies of content. Details go in files.
- **Stop at the cheapest proof.** A task is complete when a fast, targeted check passes. Run the full and slow check only one time, at the end.
- **Ask when a decision blocks the flow.** Do not guess. Record my answer as a decision.
- **Done means approved.** Work is complete only after I approve the result.
- **One folder for each task.** Put reports, diffs, logs, the delegation log and screenshots in the task folder. Exclude it from version control.
- **Keep task folders small.** Save only what a later step or the user reads. Store a log as its last relevant lines, not the full log. Compress screenshots. Overwrite a file on a new round; do not add a copy. After approval, keep only what the settings file says.

## 11. Tool rules

- Write a tool only for a gap that the audit shows.
- Use a language that runs here without installs (for example Python or Node). Keep the tool in the shared folder, not in a temporary folder.
- Write one short test for each behaviour of the tool, including one test that reads a limit from the settings file. Run the tests after each change.

## 12. Prove it

- Start a new session. Do one small real task through the `delegate` skill: `<sample task>`.
- Run the audit script on that session.
- Check that the script reports the correct duration, tokens, cost, agents and models, rounds, repeated calls, task folder size and gate result. If a value is wrong, fix the script and run it again.
- Make one workflow decision in that session. Check that one line goes to `LESSONS.md`, and that a copy of an existing lesson updates the old line.
- Check that the lessons skill did not load in a session where it was not necessary.
- Show me a before and after table. Wait for my approval before you save or commit the changes.
