Skip to content
Back to skills

Perfeval Agent

BSecurity

Measures where one agentfactory agent's wall clock and tokens go (per formula step, child agent, in-step sub-agent, wake and fidelity-gate evaluation), ranks the cost centres, and names the one lever with the largest measured saving, its controller, and the query that proves it after a change. On request it applies a formula-controlled lever under the formula-edit rules. Use when the user says "/perfeval-agent <agent>", "why is <agent> slow", "where do the tokens go", "make <formula> faster o...

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added October 5, 2026
ai-agentspythongoshellbashdockergitperformance

Works with

  • cli

Security analysis

B85/100
  • highPerforms destructive filesystem operations

Pro shows the line behind each finding and how to fix it

Scanned November 30, 2026

npx -y skills add stempeck/agentfactory --skill perfeval-agent --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Perfeval Agent?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Perfeval Agent
[![Security: B — Skills Directory](https://www.skillsdirectory.com/api/skills/stempeck-perfeval-agent/badge)](https://www.skillsdirectory.com/skills/stempeck-perfeval-agent)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: perfeval-agent
description: Measures where one agentfactory agent's wall clock and tokens go (per formula step, child agent, in-step sub-agent, wake and fidelity-gate evaluation), ranks the cost centres, and names the one lever with the largest measured saving, its controller, and the query that proves it after a change. On request it applies a formula-controlled lever under the formula-edit rules. Use when the user says "/perfeval-agent <agent>", "why is <agent> slow", "where do the tokens go", "make <formula> faster or cheaper", or "find the slow steps". It never auto-files an issue.
---

# Evaluate Agent Performance

The deliverable is a **Verdict**: the cost centre with the largest measured saving, the mechanism
behind it, who controls it (formula, factory config or Go code), the script output every number
comes from, and the query that proves or disproves a change. Run every measurement yourself.

**Argument:** an agent name, or a formula file name (then use the agent whose `.formula` in
`af agents list --json` matches it).

## Setup

Run every command inside the factory environment (the container or host the agent runs in).
From another host, run each command through that environment's shell
(`docker exec -u <user> <container> bash -c 'cd <ROOT> && …'`). Never run `af` on a machine that
is not the factory, and never run `claude` inside the factory environment: a launch there can
purge the old session transcripts this skill measures.

The factory root is the bootstrapped project at `~/af/<project>`; every agent runs there and
every measurement comes from there. `~/projects/<agentfactory>` is the agentfactory source that
installs af and generates agents: it is not a factory root, so never measure, read config or
edit there, even though it has an `.agentfactory/` directory of its own.

```bash
AF=$(command -v af || echo "$HOME/.local/bin/af")
AGENT=<name>
for f in "$HOME"/af/*/.agentfactory/telemetry/steps/"$AGENT".jsonl; do      # several projects: most records wins
  [ -f "$f" ] && echo "$(wc -l < "$f" | tr -d ' ') ${f%/.agentfactory/*}"; done | sort -rn
ROOT=<the first directory listed>; cd "$ROOT" && $AF root          # must print ROOT; none listed: stop
W=$(mktemp -d "$HOME/.cache/perfeval.XXXXXX")     # the only directory this skill writes to or deletes
# copy <this skill's directory>/scripts/perfeval.py to $W (from macOS into a container:
#   COPYFILE_DISABLE=1 tar --no-xattrs -cf - -C <skill dir>/scripts perfeval.py | docker exec -i -u <user> <container> tar -C <W> -xf -)
for py in /usr/bin/python3 python3; do command -v "$py" >/dev/null && "$py" "$W/perfeval.py" selftest | tail -n 1; done
P="python3 $W/perfeval.py"                        # every selftest line must say "all checks passed"; if not, stop
STEPS=$ROOT/.agentfactory/telemetry/steps/$AGENT.jsonl
FORMULA=$($AF agents list --json | jq -r --arg a "$AGENT" '.[] | select(.name==$a) | .formula')
STORE=.agentfactory/store/formulas/$FORMULA.formula.toml          # the running copy, relative to ROOT
$AF telemetry status
$AF telemetry report --agent "$AGENT" --json > "$W/report.json"
C="--report $W/report.json --agent $AGENT --steps-log $STEPS --transcripts $HOME/.claude/projects"
# add --transcripts <dir> for each transcript backup root; a session in several roots counts once
```

Stop and say so if telemetry is off, the report has no rows, or `$FORMULA` is empty. At the end,
`rm -rf "$W"` and nothing else. `tokens` runs `$AF telemetry report --json --instance <id> --requests`
once per run it reports (its `--af` defaults to the same `af` as `$AF`); to read saved payloads
instead, pass `--requests-file <id>=<file>` for each run, and then it runs no `af` at all.

Copy this checklist; tick an item only when its output is in your notes:

```
perfeval progress:
- [ ] 0 Setup: selftest passed; telemetry on; FORMULA resolved
- [ ] 1 steps + runs: step profile, every run's identity, the text each run ran, today's text
- [ ] 2 Ownership: sling grep on both texts; children for each child agent
- [ ] 3 tokens: both ranked blocks; reconcile --run on one covered run
- [ ] 4 The runs' text and today's text read for the top centres; their diff checked
- [ ] 5 time on the top wall-clock step: main, --subagents, pauses of the 2 slowest covered runs
- [ ] 6 tokens --detail (and --held-file) if sub-agent share >= 20%
- [ ] 7 gate if the gate-cost share >= 5%
- [ ] 8 runs --values if an artifact's size may explain a change
- [ ] 9 Verdict with every field; offer apply and /github-issue; stop
- [ ] 10 Apply, only when asked; end with the AFTER plan
```

## Hard rules

1. **Every measured number comes from `perfeval.py` or `af telemetry` output pasted into
   LEDGER.** When a question needs a measurement neither provides, write the script in `$W`
   (never anywhere else, `/tmp` included), quote it in REPRODUCTION, and cross-check one of its
   totals against a `perfeval.py` figure before using it. The only arithmetic in the Verdict is
   PROJECTED SAVING's, written out on pasted figures in the centre's own unit
   (`rank-2 row per-run $1.72 × 0.5 = $0.86`; for a no-rate centre, `per-run 1720000 tokens × 0.5 =
   860000 tokens`).
2. **Rank before you name.** The VERDICT's centre is a row of one of `tokens`' two ranked blocks
   (or `steps`' table for wall clock), quoted with its block and rank. Centres with a rate are
   ranked by dollars; centres with any no-rate figure are ranked by tokens under "no rate: ranked
   by tokens"; never compare a row of one block with a row of the other. Call it largest or
   dominant only if it is rank 1 of the priced block (or rank 1 of the no-rate block, saying so).
   Call a centre context-bound only if its dominant part is cache_read, output-bound only if it
   is out; "mixed" means neither. The dominant part is the class holding at least half of the
   centre's dollars in the priced block and of its tokens in the no-rate block; the column header
   names which. Call a step wait-, model- or tool-bound only if `time` names that category
   dominant (>= 50%).
3. **List every lever, then choose by expected saving.** LEVERS has one line per candidate:
   every mechanism found inside the rank-1 centre, each other ranked row >= 10% of its block, and
   the gate lever when the gate cost is >= 5%. Each line gives the rows it removes × the fraction
   removed = the saving per run that has it, the base rate (runs that have it / runs, from `runs`
   or the ledger), the expected saving per run (saving × base rate), the controller, and whether
   today's text already changes it. Every line is written in its centre's unit: dollars for a
   priced centre, tokens for a no-rate one; lines in different units are not compared with each
   other, and the choice between them is stated with both figures. A `--held-file` figure is an
   estimate in tokens and is compared only with token figures. The VERDICT's lever is the line
   with the largest expected saving; the runner-up is named with its figure. When two lines
   remove the same cost, keep the one that keeps every check today's text requires. No rows, no
   saving.
4. **Runtime claims cite code or a count**: the greps in "Run model" below, or a count a command
   printed. Reading the formula shows what the agent was told, not what it did.
5. **A correlation or a change between texts is a lead.** Before it reaches the Verdict, check the
   confounds on runs either side: text, model, af build and host version (`runs`), and workload
   size. Say what you checked.
6. **Median, not mean; state coverage** (N profiled / N existing) for every population figure.
   When coverage is under half, compare the covered runs' medians with `steps`' population
   medians and say in the VERDICT line whether they are typical.
7. **The harness always replies.** Every wake produces a response turn; a lever removes wakes or
   makes the response short and inside the contract the grader sees, never "do not reply".
8. **Thinking text is persisted empty**; use `time --pairs` to see what preceded long pauses.
9. **If a rule was broken, retract it in the same response** with the corrected figure.

## Phases

**1. Profile and identity.**

```bash
$P steps --report "$W/report.json" --steps-log "$STEPS"      # a renamed step stays one row (step id)
$P runs --report "$W/report.json" --steps-log "$STEPS" --step "<top step fragment>" --repo "$ROOT" --formula "$STORE"
# when the project is agentfactory itself, also --formula internal/cmd/install_formulas/$FORMULA.formula.toml
```

`steps` ranks steps by median span (`closed`, `gate-waiting` and `ended` rows are each a whole
span) with each step's share of a median run and the run holding its maximum, and lists excluded
rows (INTERRUPTED) and the run lines (the improvement session and the time outside every step),
which are not steps and not part of the median run. `runs` lists every run from telemetry alone (model, formula digest,
af build, host version, sub-agent launches, gate flags), groups runs by the commit whose file
matches their digest ("uncommitted" means a store edit), states whether today's text has run, and
lists the commits since the last run. If the project's git does not track the store copy, it says
so: older runs' text can then only be compared by digest, not read.

**2. Ownership.** Wait-shaped steps (Dispatch, Await) mean a child agent owns the time.

```bash
grep -n "sling --agent" "$ROOT/$STORE"                                              # today's text
git -C "$ROOT" show <commit the runs ran>:<its path> | grep -n "sling --agent"        # the runs' text
$AF telemetry report --agent <child> --json > "$W/<child>.json"
$P children --report "$W/report.json" --child "<child>=$W/<child>.json"
```

Each child run is tied to the parent step it ended in, the step that waited for it. A step whose
median matches the child's span with an end delta near zero is the child's run: start over with
`AGENT=<child>`. No sling lines means no children; quote the empty grep in OWNERSHIP. In-step
Agent-tool sub-agents are not children; they are measured in phases 3 and 6.

**3. Tokens.** `$P tokens $C`. Every figure is the command's `cost`: per step and run, per run
line (the improvement session and the time outside every step) and for graders (the `grading`
parts, fidelity and quality gate alike). A figure whose cost is not final carries its state
(`settling`, `partial`, `unverified`); `open`, `before_upgrade` and `unmeasured` print in place of
a figure and are never a zero. Centres are each step across runs and each run line across runs,
in two ranked blocks: centres with a rate ranked by dollars, which sum to the dollar TOTAL, then
"no rate: ranked by tokens", whose tokens the TOTAL line states apart. Each row gives its share
of its block, per-run figure, dominant part, tokens by part (own, subagent, nested, grading) and
controller. Below them: graders by step, the summary by part, the gate cost (graders plus
main-session turns started by gate mail), and the finer cuts grouped from the `--requests` rows:
main-session turns by the prompt that started them (a turn is all its work until the next
prompt), step × prompt, and sub-agents by role. A run whose `--requests` rows could not be read
is named and left out of the finer cuts only.

Then check the figures against the transcripts for one covered run, preferably the rank-1
centre's longest:

```bash
$AF telemetry report --instance <id> --requests --json > "$W/req-<id>.json"
$P reconcile --run <id> --report-file "$W/req-<id>.json" --requests-file "<id>=$W/req-<id>.json" \
  --transcripts "$HOME/.claude/projects/<the agent's project directory for that run's worktree>"
```

`reconcile --run` recounts the run from its transcripts with its own attribution and prints one
line per difference (a request the command missed or placed in another step or part, a step,
part, run line, agent or model whose tokens differ, a dollar sum off by more than $0.01, a step
stamp outside the `af prime` or `af done` call that wrote it), what it did not recount, the
superseded commits, and `reconcile: N differences`. Any difference goes in INCIDENTS, and a figure
it disputes is not a basis for the Verdict.

**4. Read both texts.** The text the runs ran (phase 1 names its commit) and today's text, which
phase 10 would edit:

```bash
git -C "$ROOT" show <commit>:<path> > "$W/ran.toml"
git -C "$ROOT" diff --no-index "$W/ran.toml" "$ROOT/$STORE"
```

If today's text has never run, a mechanism the diff changed is not measured on today's text; its
saving is re-derived against today's text or reported as unknown in TEXT. For each top centre,
note what the text asks for: actions, sub-agents and what each writes, the turns it causes,
exit criteria. Checks earn their runtime: the lever is never "verify less".

**5. Time.**

```bash
$P time $C --step "<fragment>"                    # main session; also states time no transcript covers
$P time $C --step "<fragment>" --subagents        # agent-minutes: parallel sub-agents summed
$P time $C --step "<fragment>" --instances <the 2 slowest covered runs> --pairs
```

The gap before an entry is charged to it: model time before thinking/text/tool_use, command
execution before a tool result, waiting before a prompt or queued-prompt record, hook execution
before a hook attachment (the fidelity grader runs in the Stop hook). If runs disagree, look at
the divergent ones before emitting anything.

**6. Sub-agents.** `$P tokens $C --detail` lists every sub-agent (step, start and end clock time,
minutes, calls, output tokens, write share, peak context, dollars), which shows which ran in
parallel and which waited for another, and the files they read, largest first; "tool output"
entries are command output the harness saved to a file, not inputs the formula supplies. Then
`$P tokens $C --held-file <largest file read that is not a tool output>` estimates, in tokens,
what keeping that file in context took: its tokens written to the cache once, plus read again on
every later call. It is an estimate from the file's size, compared only with token figures.

**7. Gate.** `$P gate --agent "$AGENT" --transcripts <same roots>` prints evaluations and flags per
step (truncated = the grader saw only the first 4096 bytes), clusters flags by the directive the
grader quoted, and counts wakes from gate mail versus echoes of the agent's own replies. Add
`--phrase "<sentence the flagged turns relied on>"` to see whether that sentence was inside the
text each grader saw, and `--formula <file>` (today's `$ROOT/$STORE`, or the runs' `$W/ran.toml`)
to see at which byte of each step that sentence sits in a text no grader has seen yet. For a cluster of in-contract turns, the lever is step text inside the window;
its saving is the cluster's flags × (the graders' figure ÷ grader sessions + the gate-mail turns'
figure ÷ gate-mail prompts), both from `tokens`, in dollars when both are priced and in tokens
otherwise. Clusters can name one instruction in two wordings; merge them only after reading the text.

**8. Artifact size.** Add `--values <TSV: ISO_TIME<TAB>VALUE>` to `runs`; each value lands on the
run whose window holds it, others are dropped. Build it with `wc -c` per run's artifact, or from
git when worktrees are gone: `git -C "$ROOT" log --all --format='%H %cI' -- '*/<name>'`, then
`git -C "$ROOT" cat-file -s <commit>:<path>`.

## The Verdict (phase 9)

Deliver exactly one finding with every field. Every figure it quotes names its unit: dollars
(`$`), tokens, or minutes.

```
VERDICT: <agent>'s <centre, quoted from its ranked block with the block and its rank> is <X>-bound, not <Y>-bound.
TEXT: <digest/commit measured; today's digest and its run count; whether their diff touches the mechanism, quoted>
MECHANISM: <one falsifiable sentence: what structurally causes the cost>
CONTROLLER: <formula | factory config | Go code>; on the critical path (before WORK_DONE)? yes/no
OWNERSHIP: <children table, or the empty sling grep>
STATUS SEMANTICS: <the code behind every telemetry status relied on>
LEDGER: <the pasted rows the verdict and saving rest on, each block's top 5 with its unit; coverage; the reconcile --run result>
LEVERS: <one line per candidate, in its centre's unit: rows × fraction = saving per run that has it; base rate; expected per run; controller; changed in today's text?>
EVIDENCE: <dominant category and share, r, two exemplar runs, the confounds checked>
PROJECTED SAVING: <the chosen LEVERS line restated, with its unit: a ceiling; against today's text, or "unknown" and why>
INCIDENTS: <outlier rows and their causes, apart from the profile; every reconcile difference>
OTHER CENTRES: <every other ranked row >= 10%, with controller; reported, not acted on>
AFTER: <the phase 10 commands and how both arms will be produced>
REPRODUCTION: <every command above, verbatim>
```

Then stop and offer: apply the lever (only if CONTROLLER is formula), or `/github-issue` with the
Verdict. Never apply or file without the operator saying so.

## Applying a lever (phase 10, only when asked)

- **Edit in the factory root only.** The running copy is `$ROOT/$STORE`. When the project is
  agentfactory itself it also holds the source, `internal/cmd/install_formulas/<f>.formula.toml`:
  `diff` the two first and stop on a difference (the store may hold a self-edit); otherwise edit
  the source, run `make sync-formulas` in `$ROOT`, and `diff` again. In any other project, edit
  the store copy and report that promotion into the agentfactory source and `af install --agents`
  are the operator's (USING_AGENTFACTORY.md); never edit `~/projects/<agentfactory>`.
- **Validate the edit directly, not with Go tests**: the template-parity tests turn red on any
  description or title change until the operator regenerates the agent. TOML:
  `python3 -c 'import sys, tomllib; tomllib.load(open(sys.argv[1], "rb"))' <file>` (Python 3.11+)
  or `$AF formula show <f> --json` in the factory. Run every changed shell block under `bash`
  against a fixture; awk must work in mawk and BSD awk (no `{m,n}`); commands must exist on Linux
  and vanilla macOS.
- **Instructions only.** No dates, run or issue ids, durations, measured figures or "(Why: …)"
  notes, in prose or TOML comments: step text is in context on every turn of every run. State the
  bytes added and removed (`wc -c`).
- **No stale text.** Update the description, titles, anti-pattern tables, exit criteria,
  deliverable lists and later steps the change contradicts. Name `TestFormulaTemplateDrift` and
  `TestDeployedAgentIdentityMatchesEmbeddedTemplate` as red until agent-gen. Never edit role
  templates, an agent's `CLAUDE.md` or agent-gen output.
- **The grader's window.** Describe every turn the step causes (launch, waits, later Agent calls,
  gate-mail handling) inside the first 4096 bytes of its description.
- **Never verify less**: a lever may move, pipeline or shard a check, never drop or narrow it.
  Every sub-agent a step launches finishes before that step's `af done`.
- **Scope.** Apply every measured formula lever or decline it with a ledger row. Centres controlled
  by config or code go in OTHER CENTRES, unchanged. Never commit.
- **AFTER plan.** Both arms are fresh runs, five on the text without the change and five with it,
  under the same af build, host and checkout (`compare` voids anything else):

```bash
$AF telemetry compare --formula "$FORMULA" --surface a --before <5 ids> --after <5 ids> --json
$P steps --report "$W/report.json" --steps-log "$STEPS" --instances <5 before ids>   # and the 5 after ids
$P tokens $C --instances <5 after ids>                            # the removed rows must be gone
$P gate --agent "$AGENT" --report "$W/report.json" --steps-log "$STEPS" --transcripts <roots> --instances <ids>
```

`compare` judges output plus sub-agent tokens over closed steps and passes only when
median(after) < min(before); wall clock is judged with the two `steps` arms by the same bar. A
cost present in some runs only is judged by its presence per run. Nothing is measured until both
arms exist.

## Run model

Line numbers drift; re-run a grep before citing it.

| Fact | Where it is decided |
|---|---|
| The factory root is `~/af/<project>`; `~/projects/<agentfactory>` only installs af and generates agents | USING_AGENTFACTORY.md, "Directory layout" and "Batch regeneration" |
| `closed` and `gate-waiting` are the same full span (gate-waiting = `af done --phase-complete`); `ended` is a step a cost boundary closed without `af done`, also a full span | `grep -n StatusGateWaiting internal/cmd/done.go`; span: `grep -n "func telemetryStepSpan" internal/cmd/*.go`; `grep -n "reportRowStatusEnded =" internal/cmd/telemetry_cost.go` |
| Fidelity graders' input starts "Current step:", quality graders' "Assistant response:" | `grep -n 'EVAL_INPUT="' hooks/fidelity-gate.sh hooks/quality-gate.sh` |
| `INTERRUPTED` is a report-time label for an open step whose session was recycled | `grep -n "statusInterrupted =" internal/cmd/telemetry.go` |
| Every run records its formula digest, model, af build; step closes record host version, sub-agent launches, gate flags | `grep -n "FormulaDigest string\|HostVersion  *string" internal/telemetry/event.go` |
| The Stop hook runs the fidelity gate at every turn end | `grep -n fidelity-gate.sh internal/claude/config/settings-autonomous.json` |
| The gate grades only turns whose step is ready; it logs each exit in the worktree's `.runtime/fidelity_debug.log` | `grep -n 'EXIT[0-9]*:\|AGENT_RUNTIME=' hooks/fidelity-gate.sh` |
| The grader is haiku and sees the step title, the first 4096 bytes of the description, the response and the turn's tools | `grep -n "head -c 4096\|model haiku" hooks/fidelity-gate.sh` |
| A flag mails STEP_FIDELITY to the agent itself (a wake); the threshold escalates to the supervisor once per step | `grep -n "STEP_FIDELITY\|ESCALATION_THRESHOLD" hooks/fidelity-gate.sh` |
| The watchdog nudges a pane unchanged for `--silence-timeout` | `grep -n "doing your job\|silence-timeout" internal/cmd/watchdog.go` |
| WORK_DONE goes to the dispatcher before the improvement hook fires: the tail is off the critical path | `grep -n "sendWorkDoneMail(\|Continuous-improvement hook" internal/cmd/done.go` |
| A boundary handoff respawns the pane, so sub-agents do not survive a step boundary | `grep -n "boundaryHandoffExec = func\|return respawnSession" internal/cmd/done.go` |
| `compare` metric and pass rule; it voids arms whose build, host, checkout or digest differ | `grep -n "m.Metric =" internal/telemetry/protocol.go`; `grep -n "func ProtocolCompare" internal/tokenomics/protocol.go`; `grep -n 'compareCheck.*= "' internal/cmd/telemetry_compare.go` |

Tokens and dollars are the command's `cost`, from `af telemetry report --json`: `rows[].cost` per
step, `run_lines[].cost` per run line, the `grading` parts for graders, each with tokens by class,
requests, `usd` (a six-place decimal, `null` where a model has no rate) and `usd_by_class`;
`--requests` with one `--instance` gives one row per request, which `tokens` groups for its finer
cuts. perfeval counts no token itself except in `reconcile --run`, its independent check.
Transcripts: `~/.claude/projects/<dir>/<session>.jsonl`, `<dir>` ending
`<worktree>--agentfactory-agents-<agent>`; sub-agents under `<session>/subagents/`; grader sessions
are those whose first prompt starts "Current step:" (the fidelity gate) or "Assistant response:"
(the quality gate); `gate` reads the fidelity graders only.

## Anti-patterns

| Anti-pattern | Instead |
|---|---|
| Naming a centre "dominant" or "the largest" without its rank | Rule 2: quote the ranked table |
| Comparing a no-rate centre's tokens with a priced centre's dollars | They are separate blocks; Rules 2 and 3 |
| Reading a state printed in place of a figure (`open`, `before_upgrade`, `unmeasured`) as zero | It is no figure; say the step was not measured |
| "Context-bound", "output-bound" or "wait-bound" from judgement | Only when the table or `time` prints that category dominant |
| A saving with no rows, or on a centre the lever does not touch | Rule 3 |
| Choosing a lever without the others' expected savings beside it | Rule 3: the LEVERS field |
| Proposing a lever measured on text that today's text already changed | Phase 4 diff; the TEXT field |
| Claiming a wake or a reply loop, or "the agent should not reply" | Count it with `tokens` or `gate`; Rule 7 |
| Counting the improvement or after-last-step run line as the formula's cost | It runs after WORK_DONE; its controller is config or the late mail's sender |
| Writing or deleting anything outside `$W`, `/tmp` included | `$W` is the only directory this skill creates; ad-hoc scripts live there |
| Measuring or editing in `~/projects/<agentfactory>` | The factory root is `~/af/<project>` |

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.