In today’s newsletter:
Self-improving security for all agents!
Training an LLM to generate reliable structured output.
Implement Knowledge Distillation from Scratch
Self-improving security for all agents!
We’re giving coding agents more powerful tools every day. How are developers supposed to keep up with everything that could go wrong?
Finally, someone is applying self-improving memory to security.
Beacon (open-source) learns from risky agent behavior and helps turn those findings into guardrails across:
Claude Code
Codex
Cursor
Here’s the repo: https://github.com/Asymptote-Labs/agent-beacon
(don’t forget to star it ⭐)
Say you give your coding agent database access to debug a production issue. You want it to investigate, but you definitely don’t want it deleting customer records.
Without enforcement: The agent can go ahead and delete records.
With enforcement: Beacon blocks the deletion, and the agent can keep debugging.
All you had to do was define a guardrail in plain English.
Beacon learns from your agent sessions so a security risk uncovered in Claude Code today can become a guardrail that protects Codex and Cursor tomorrow.
Your agents get smarter with every session, and now your security can too. One agent’s mistake becomes every agent’s guardrail.
If you want to try this yourself, self-improving security is now available for free in Beacon Cloud: https://beacon.sh.
Thanks to Asymptotelabs for partnering today!
Training an LLM to generate reliable structured output
An LLM has no inherent concept of a parser. It predicts the next token from the ones before it, and well-formed JSON is only one of the continuations that fit this format.
So a number written as a string fits just as well, and so does an object with a line of explanation in front of it.
More examples and a tighter prompt do push the failure rate down, but then it stalls.
DeepSeek-R1 showed a way around this. Training a strong model used to mean annotation pipelines, preference pairs, and a team of labelers.
DeepSeek replaced all of it with one function that checks whether an answer is right. If you can define correctness via code, you do not need the rest.
That is the idea behind GRPO. Instead of learning from examples, the model learns from a reward function you write.
For each prompt, it generates a few candidate answers. The reward function scores them, and the model is pushed toward the ones that score higher.
Today, let’s use this idea to fine-tune Qwen3-8B for JSON extraction. The loop runs from a local notebook while the model trains on remote H200s.
The reward function just checks whether each output parses and matches the schema.
Schema-valid output went from 62% on the base model to 82% after training, past GPT-4.1 on the same eval at 58%.
Before we dive into code, let’s see why the obvious approach stalls.
On a side note, to dive deeper, we’ve covered reinforcement learning from scratch in our 13-part course, building from rewards and value functions to PPO, RLHF, and GRPO.
It explains how these methods work and how they connect to training LLMs and agents.
Problems with SFT
SFT learns by copying examples. If you show it correct completions, it gets good at producing output that looks like them.
Notice the word “looks.”
This implies that looking like valid JSON and being valid JSON are different goals, and SFT only optimizes the first one.
The loss is measured token by token, and a completion with one field typed wrong scores almost the same as a perfect one.
If you add more examples, the metrics do tick up, but then they flatten, because the limit is the objective, not the data.
Once you see the problem this way, the fix becomes immediately clear. You can define what “correct” means in code, and train the model against that definition directly.
This is what GRPO does.
It swaps labeled examples for a reward function. For each prompt, the model generates a small group of answers (usually 4-8). And then the reward function scores every one of them.
The scores are normalized inside the group. The model update then reinforces the answers that scored above the group’s average.
So the model is always comparing its own outputs against each other. It learns what “more correct” means for your task, not what “more similar to an example” means.
Here is how the reward function scores three different outputs for the same prompt:
Output that doesn’t parse as JSON scores 0.0.
Output that parses but fails the schema scores 0.5.
Output that parses and matches the schema scores 1.0.
That middle score is important because without it, valid JSON with the wrong field types would score zero, the same as complete garbage.
The model would lose an important signal that a valid structure is already progressing. The 0.5 is what gives training something to climb toward.
Infra for GRPO
GRPO is heavier than SFT. On an 8B model, it needs H200s and runs for hours.
Every step, it generates several completions per prompt, scores all of them, and updates the weights. That repeats across the whole dataset, many times over.
So this is not something you can run on your laptop.
There is also a timing problem that SFT never has.
During rollout, the model samples answers from its current weights. During training, those same weights are changing underneath it.
If the inference side and the trainer fall out of sync, you sample from a stale model and train on answers that the current one would never give. This is where most custom RL setups fall apart.
Fireworks’ Training API handles both sides. You can write the training logic in Python on your own machine, and the infrastructure handles the rest.
It provisions the GPUs, runs the forward and backward passes, saves checkpoints, and resyncs the inference deployment after every step.
The setup is three steps:
Write the reward function
Upload the dataset
And configure the run.
Let’s go through each one.
The training loop
Fireworks documents the full setup in their Training API docs. That includes rl_loop, the recipe that runs the entire GRPO loop.
Clone the cookbook and install the training dependencies:
git clone https://github.com/fw-ai/cookbook.git
cd cookbook/training && pip install -e ".[training]"
export FIREWORKS_API_KEY="your-training-scoped-key"Step 1: Write the Reward Function
This is the only place your task is defined. The SCHEMA says what a correct output looks like, and score() checks each completion against it.
For invoice extraction, we pull four fields from raw text: vendor, date, amount, and currency.
import json
from jsonschema import validate, ValidationError
SCHEMA = {
"type": "object",
"required": ["vendor", "date", "amount", "currency"],
"properties": {
"vendor": {"type": "string"},
"date": {"type": "string"},
"amount": {"type": "number"},
"currency": {"type": "string"},
},
"additionalProperties": False
}
def score(completion: str) -> float:
try:
parsed = json.loads(completion.strip())
except (json.JSONDecodeError, ValueError):
return 0.0
try:
validate(instance=parsed, schema=SCHEMA)
return 1.0
except ValidationError:
return 0.5jsonschema handles the type checks, the required fields, and any nested rules in a single call.
For a different task, like SQL or tool-call formatting, you can swap in a new schema. The score() function stays the same.
Step 2: Prepare the Dataset
GRPO does not need labeled outputs. The dataset is just the prompts you would send in production.
The model writes its own completions during training, and score() grades them as they come.
We used 200 training prompts. They cover different vendor names, date formats, amount styles, and currency codes. We also set aside 50 eval prompts.
Variety matters more than volume here. Prompts that all look alike produce a model that breaks on real invoice variation.
{
"messages": [
{
"role": "user",
"content": "Extract the following fields from this invoice:
Bill from Acme Corp, dated 2024-03-15, total $1,250.00 USD.
Return valid JSON only."
}
]
}
{
"messages": [
{
"role": "user",
"content": "Extract the following fields from this invoice:
Received from TechSupplies Inc on January 8 2024,
amount due: 340 euros. Return valid JSON only."
}
]
}Upload the dataset and wait for the READY state before the training job can use it.
from fireworks import Fireworks
client = Fireworks()
client.datasets.create(dataset_id="invoice-extraction-grpo-v1",
dataset={"exampleCount": "200"})
client.datasets.upload(dataset_id="invoice-extraction-grpo-v1",
file="./train_prompts.jsonl")
# poll client.datasets.get(...).state until "READY" before proceedingStep 3: Configure and Run the Loop
rl_loop runs the whole thing. It provisions the trainer, schedules the rollouts, syncs the weights, and cleans up when the run is done.
You connect your score() function by wrapping it and assigning it to rl_loop.reward_fn. The wrapper gets both the completion and the dataset row, so you can reach ground-truth metadata if your reward needs it.
from training.recipes.rl_loop import Config, main
from training.utils import DeployConfig, InfraConfig, WeightSyncConfig
import training.recipes.rl_loop as rl_loop
# Wire your reward function to the training loop
def invoice_reward(completion: str, row: dict) -> float:
return score(completion)
rl_loop.reward_fn = invoice_reward
cfg = Config(
base_model="accounts/fireworks/models/qwen3-8b",
dataset="invoice-extraction-grpo-v1",
max_rows=200,
epochs=1,
completions_per_prompt=4,
max_completion_tokens=256,
temperature=1.0,
max_seq_len=4096,
policy_loss="grpo",
output_model_id="invoice-extractor-v1",
infra=InfraConfig(
training_shape_id="accounts/fireworks/trainingShapes/qwen3-8b-128k",
),
deployment=DeployConfig(
deployment_id="invoice-extractor-v1",
tokenizer_model="Qwen/Qwen3-8B",
),
weight_sync=WeightSyncConfig(weight_sync_interval=1),
)
main(cfg)A few of these settings to note here:
datasetpoints to thedataset_iduploaded in Step 2. Fireworks pulls it from their storage directly.completions_per_prompt=4sets the group size for GRPO. Production runs often use 8 to 16, which gives more signal per step at a higher compute cost. Four is enough here. The reward is clear-cut enough that even a small group shows real variance between answers.weight_sync_interval=1resyncs the inference deployment after every step. That keeps rollout sampling from the exact model being trained. Production runs often set this to 4 or 8 for speed. For a short 200-step run, 1 gives the tightest feedback loop, which is what you want.One Qwen3 quirk to handle here is that it defaults to thinking mode and adds
<think>...</think>blocks before the answer. Strip them at eval time withcontent.split(”</think>”)[-1].strip(). Suppress them in training by adding/no-thinkto the system prompt. Otherwise, the reward function reads the reasoning text instead of the JSON, and scores everything 0.0.
Results on the held-out eval
Base Qwen3-8B scores 62% schema-valid on the 50 held-out prompts. After GRPO training on Fireworks H200s, the fine-tuned model hits 82%.
That is past GPT-4.1 on the same eval, which lands at 58%.
Here is the baseline run first, on the 50 prompts the model never trained on.
And here is the same eval after training.
The trained model runs on a Fireworks serverless endpoint, at a fraction of GPT-4.1’s per-token cost. Latency is lower too, since the output is short and predictable.
The real difference shows up on messy inputs. A prompted general-purpose model starts to slip, while the trained model holds, because it learned the constraint instead of the examples.
This pattern works for any task where you can check correctness in code.
SQL that has to parse, API responses that must match a shape, tool calls, and code that has to pass a linter.
If you can score an output, you can train a model to chase that score.
What DeepSeek-R1 proved at the frontier scale holds for your own small task. The model you get has practiced your definition of correct, not memorized examples of it.
The full code is in the repo below. That includes the reward function, the training config, the dataset builder, and the eval script.
You can find the fine-tuning code in our repo here →
To dive deeper, our 13-part RL course builds the foundations behind these methods, then applies them to LLM training and agent workflows.
Implement knowledge distillation from scratch
Model performance is rarely the only factor in determining which model will be deployed.
This is because we also consider several operational metrics, such as:
Inference Latency: Time taken by the model to return a prediction.
Model size: The memory occupied by the model.
Ease of scalability, etc.
Knowledge distillation is a common technique to compress ML models before deployment.
Let’s learn how to implement it!
We implemented 5 more techniques to compress ML models and reduce costs here: Model Compression: A Critical Step Towards Efficient Machine Learning.
What is knowledge distillation?
In a gist, the idea is to train a smaller/simpler model (called the “student” model) that mimics the behavior of a larger/complex model (called the “teacher” model).
This involves two steps:
Train the teacher model as we typically would.
Train a student model that matches the output of the teacher model (there are some other types of knowledge distillation techniques as well).
DistillBERT, for instance, is a student model of BERT.
DistilBERT is approximately 40% smaller than BERT.
But it retains approximately 97% of the BERT’s capabilities.
Next, let’s look at the implementation.
Knowledge distillation implementation
Imagine we have already trained a CNN model on the MNIST dataset:
Its epoch-by-epoch training loss and validation accuracy are depicted below:
Let’s define a simpler model without any convolutional layers:
Since it’s a classification model, it will output a probability distribution over the <N> classes.
Thus, we can train the student to match the probability distribution of the teacher on all samples.
KL divergence (also used to train tSNE) can be used as a loss function.

It measures how much information is lost when we use distribution Q to approximate the distribution P.
Thus, in our case:
P→ probability distribution from teacher.Q→ probability distribution from student.
The loss function is implemented below:
Finally, we train the student model:
The following image compares the training loss and validation accuracy of the two models:
The performance of the student model is not as good as the teacher model, which is expected.
However, it is still promising since it’s a simple feed-forward network.
Also, the student model is 35% faster than the teacher model, which is a significant increase in the inference run-time of the model for a 1-2% drop in performance.
That said, one downside of knowledge distillation is that one must still train a larger teacher model first to train the student model.
This may not be feasible in a resource-constrained environment.
Today, we only covered knowledge distillation.
We implemented 5 more techniques to compress ML models and reduce costs: Model Compression: A Critical Step Towards Efficient Machine Learning.
👉 Over to you: What are some other ways to build cost-effective models?
Good day!


















