Snowflake-Labs/agent-self-training-loop
Python
Captured source
source ↗Snowflake-Labs/agent-self-training-loop
Description: Post-train a small open-weight model on Cortex AI Gateway agent traces, scored by TruLens and trained with Cortex Training
Language: Python
License: Apache-2.0
Stars: 0
Forks: 0
Open issues: 0
Created: 2026-11-05T21:23:33Z
Pushed: 2026-11-17T16:21:01Z
Default branch: main
Fork: no
Archived: no
README:
The Self-Training Loop
Companion code for the post *The Self-Training Loop: How Periodic Training on Agent Traces Drives Agent Improvement*.
The same loop is also a recipe in cortex-training, built on its shared helpers and SFT runner: `recipes/self_training_loop` (draft PR).
Every run of an agent produces a record of the task, the steps it took and the answer it gave. If those records are kept and scored, the good ones are training data for the same tasks. Run that periodically and it becomes a loop: each trained model answers the traffic better than the last, so it produces better training data for the next. The loop has four steps. The first runs once; the other three repeat:
1. Run the agent on a larger seed model. 2. Store every trace. 3. Evaluate each trace with reference-free LLM judges to find the good ones. 4. Train a small open model on the best traces, deploy it, and repeat steps 2 to 4 with its own traces.

Before a new version takes production traffic, the same judges score it on held-out tasks against the model currently serving. If it passes them less often across the held-out tasks, the current model keeps serving and the new version only generates the next set of training traces. Otherwise it's promoted, and its production traces feed the next pass.
Everything runs in one Snowflake account. A LangGraph agent is traced by TruLens, whose app, tool and eval spans land in `AGENT_TRACE_TABLE` next to the spans of Cortex AI Gateway. SQL selects the runs the judges scored well, and Cortex Training trains Qwen3-8B on them.

The seed model (glm-5.3, through the gateway) answers the baseline training tasks once. Qwen3-8B v1 trains on its judge-approved traces. Every later version is trained on the seed traces plus the judge-approved runs of the versions before it. No reference answer is used to choose training data or to decide which version to promote.
Result
Held-out tasks, 2 runs each. After v1 was trained, four task types it had never seen were added (the drift tasks); no seed traces exist for them. Judge pass rate is the share of runs that meet every cutoff in TRAINING_SET_JUDGED. Accuracy against the reference answers is measured only to check the judges. Both come from TruLens eval spans in AGENT_TRACE_TABLE ([sql/07_judge_results.sql](sql/07_judge_results.sql)). An errored run counts as a failure in the pass rate and is left out of accuracy. 28 of glm-5.3's held-out runs first failed on Cortex rate limits (HTTP 429); they were rerun as glm-eval-v2-retry (batch_run.py --rerun-errors-of glm-eval-v2), and the glm-5.3 row uses the reruns in their place.
| Model | Judge pass: baseline | Judge pass: drift | Judge pass: overall | Accuracy: baseline | Accuracy: drift | Accuracy: overall | |---|---|---|---|---|---|---| | glm-5.3 (seed) | 84.7% | 86.7% | 85.4% | 98.0% | 96.7% | 97.5% | | Qwen3-8B v1 | 62.2% | 13.3% | 43.7% | 89.6% | 34.5% | 68.8% | | Qwen3-8B v2 | 72.4% | 45.0% | 62.0% | 88.5% | 76.7% | 84.0% | | Qwen3-8B v3 | 90.8% | 76.7% | 85.4% | 89.8% | 93.3% | 91.1% |
Base Qwen3-8B scores 13.7% on the baseline held-out tasks, which is why the loop needs a seed.
The promotion gate compares overall held-out judge pass rate with the serving model. v1 (69 of 158) and v2 (98 of 158) are not promoted. v3 is promoted at 135 of 158, the same as glm-5.3: ahead on baseline (89 of 98 against 83), behind on drift (46 of 60 against 52). v3 is less accurate than glm-5.3 against the reference answers (91.1% against 97.5%).
Training data per version:
| Version | Trained on | Training examples (one per assistant turn) | |---|---|---| | v1 | 321 seed traces | 1,154 | | v2 | 321 seed + 401 from v1 | 2,469 | | v3 | 321 seed + 401 from v1 + 552 from v2 | 4,166 |
Each version answers the same 316 training tasks 4 times. v1 had a judge-approved run on 64 of the 130 drift training tasks, v2 on 116. Checked against the reference answers afterwards, 97.5% to 99.7% of the runs the judges kept were correct.
How it works
- Tracing.
make_tee_exporterin [agent_lib.py](agent_lib.py) sends each TruLens span to the local TruLens store, which feedback computation reads, and to the gateway's OTLP endpoint/telemetry/v1/traces. App, tool andeval_root/evalspans land inAGENT_TRACE_TABLEwith the gateway's own chat spans. Gateway spans carry full prompts and responses (logging.capture_payload.request_response: true, [sql/01_gateway_spec.sql](sql/01_gateway_spec.sql)) and join TruLens records on thex-snowflake-ai-gateway-conversation-idheader. - Small-model runs. Qwen3-8B is served from a Cortex Training sampling job (
--sampler JOB:CKPT), not through the gateway, so there is no gateway payload for those runs.make_trajectory_recorderwrites each run's messages as JSON on a plain OTel span (agt.trajectory), exported the same way. [sql/05_self_trajectories.sql](sql/05_self_trajectories.sql) buildsTRAJECTORIESrows from it. - Judges. TruLens
answer_relevanceand the trace-level agent judgeslogical_consistency,execution_efficiency,tool_selectionandtool_calling, run byclaude-sonnet-4-6through the TruLens Cortex provider. Acorrectnessfeedback compares theFINAL ANSWER:line with the SQL reference answer, for measurement only. - Selection (
TRAINING_SET_JUDGEDin [sql/05_self_trajectories.sql](sql/05_self_trajectories.sql)). Train split, clean finish,logical_consistency= 3 of 3, relevance, tool selection and tool calling at least 2 of 3,...
Excerpt shown — open the source for the full document.