The loop that learns|INEMA.CLUB-PRO

Lesson 7 · 18 min

Observability: traces and cost per cycle

A loop that runs on its own must leave a trace: how long each cycle took, how many tokens it spent, what it decided and why. Without it, an error loop or a cycle that blows your quota only shows up at month's end. In this lesson you record one line per cycle and use it to get cost per cycle, cost per keep, and the alerts.

Start the lesson
A panel with several analog gauges and a continuous paper recorder drawing lines, with a needle with a yellow tip.

In this lesson you will

  1. 1One JSON line per cycle: the minimum worth keeping
  2. 2Cost per cycle and cost per keep
  3. 3Alerts: ceiling, cycles without a keep, repeated identical errors
  4. 4Real tokens from a Claude Code call

Goal: record a trace per cycle (time, tokens, verdict) and use it to get cost per cycle, cost per keep, and the meta-agent's alerts.

01From the cycle to the alert

The same role as the LOOP-R Meta-agent: it watches the system, not the problem, and only reports. Click each block.

A trace without tokens is still useful: time, status, and reason already show an error loop and stagnation.

The alert does not stop the loop: what stops it is the course 2 ceiling (loop-longrun.sh) or the person. Here, it is reading the trace.

02Rehearsal: six cycles, two problems

Fake agents with made-up tokens: one expensive cycle and three identical failures (like an agent with no response because of the session limit).

Terminal · cost-free rehearsal
K=~/projetos/loop-r-eng-aprende-v72/kit
E=${TMPDIR:-/tmp}/ensaio-aprende/a7; rm -rf $E; mkdir -p $E
bash $K/novo-sistema.sh $E/sis > /dev/null; export RASTRO=$E/rastros.jsonl
c() { TOKENS_FALSOS="$2" AGENTE="bash $K/agente-falso.sh $1" bash $K/experimento.sh $E/sis "$1" > /dev/null; }
c comenta "8000 500"; c sinonimos "9000 700"; c estraga "41000 3000"   # made-up tokens for the rehearsal
c falha "0 0"; c falha "0 0"; c falha "0 0"
echo "## uma linha do rastro"; sed -n 2p $RASTRO
echo "## custo por ciclo"; python3 $K/custo-ciclo.py $RASTRO --teto 50000; echo "saída: $?"
Example from the repository
## uma linha do rastro
{"ciclo": 2, "descricao": "sinonimos", "segundos": 0, "tokens_entrada": 9000, "tokens_saida": 700, "status": "keep", "motivo": "melhorou"}
## custo por ciclo
ciclo  status   segundos  tokens(ent+saída)  motivo
    1  discard         0               8500  não melhorou
    2  keep            0               9700  melhorou
    3  discard         0              44000  não melhorou
    4  erro            0                  0  agente falhou (saída 1): ERRO: sem resposta do m
    5  erro            0                  0  agente falhou (saída 1): ERRO: sem resposta do m
    6  erro            0                  0  agente falhou (saída 1): ERRO: sem resposta do m
total: 6 ciclos · 1 keep · 62200 tokens
custo por keep: 62200 tokens
ALERTA: ciclo 3 usou 88% do teto de 50000 tokens
ALERTA: 3 erros seguidos iguais: agente falhou (saída 1): ERRO: sem resposta do modelo (limit
saída: 1

The tokens in this rehearsal are made up (the TOKENS_FALSOS variable); time, status, and alerts come from the real script. The error loop is the case that costs the most without showing up: in course 1, the real judge stopped at the session limit from the 16th call on.

03The tokens of a real call

agente-claude.sh asks claude -p --output-format json and writes to the trace the sum of input (with cache) and output. One call through your subscription.

Terminal · real, one call
K=~/projetos/loop-r-eng-aprende-v72/kit
E=${TMPDIR:-/tmp}/ensaio-aprende/a7      # the rehearsal above
RASTRO=$E/rastros.jsonl AGENTE="bash $K/agente-claude.sh" bash $K/experimento.sh $E/sis "claude"; echo "saída: $?"
tail -1 $E/rastros.jsonl | python3 -c 'import json,sys; d=json.load(sys.stdin); print("tokens:", "sim" if (d["tokens_entrada"] or 0) > 0 else "não", "·", d["status"])'
Example from the repository
ciclo 7 · keep · 0.800 → 1.000 · melhorou
saída: 0
tokens: sim · keep

The line written to the trace in this run: 10 seconds, 247,492 input tokens (adding cache reads and writes) and 364 output tokens. The request is under 2 KB: almost all of the input is the context that the Claude Code session loads on this machine. One measurement, Claude Code 2.1.303; yours changes with your instructions, plugins, and MCPs.

04What is already measured at INEMA

Two local cases, with the date of each number.

1. LOOP-R example

Each cycle of vendas-whatsapp has one custo.md per agent. The four cycles add up to about 1.7 million tokens for one promotion (the example's README).

Synthetic sales data, real agents: the cost is real, the effect was designed.

05Practice

Rehearsal, real, and your loop.

E19 · The two alerts

10 min

Run the rehearsal.

Done when: ALERTA: ciclo 3 usou 88% do teto, ALERTA: 3 erros seguidos iguais, and saída: 1.

E20 · Real tokens

5 min

Run the real block right after the rehearsal.

Done when: the last printed line starts with tokens: sim; if it comes out as tokens: não · erro, the agent got no response (session limit): wait and run again.

E21 · Trace in your loop

20 min

Hook RASTRO into your experiment (or write the same JSON line at the end of each cycle of your loop) and set the per-cycle token ceiling from the real measurement in E20.

Done when: python3 $K/custo-ciclo.py [your trace] --teto [your ceiling] lists one cycle per line and ends in sem alerta or in an ALERTA: that you can explain.

06Lesson quiz

Question 1 of 4

Six cycles, one keep, 62,200 tokens in total. What is the cost per keep?skill

07In three sentences

One lineEach cycle records time, tokens, status, and reason; no prompt text.
Cost per keepTotal tokens over the improvements that stayed; it includes the attempts that didn't stay.
AlertsCeiling, cycles without a keep, and repeated errors in a row; the alert reports, and the ceiling and the person stop.

Going further

Previous lessonNext lesson