Blog / 23 November 2026

Deep dive · 23 November 2026 · 3 min read

How to run Hugging Face TRL agents in kosa8

TRL, Hugging Face's library for post-training language models, can train a model that uses tools: its GRPOTrainer takes an environment_factory and gives each generation its own environment. When that environment runs code the model wrote, it should not run on your training box. kosa8's Python SDK ships an adapter that makes each of those environments a kosa8 Cloud sandbox.

Hugging Face TRL sandboxGRPOTrainer environment_factoryRL training sandboxagentic RLkosa8 TRL

01What TRL's environment_factory expects

GRPOTrainer makes one environment per generation from a class it calls with no arguments. It calls reset(**row) with the dataset row's columns at the start of each episode, turns the class's other public methods into tools for the model, feeds a tool's exception back to the model, and hands the environments to your reward functions.

The adapter in kosa8.rl.trl builds that class for you. Its tools are bash(command) and write_file(path, content), and each episode gets its own sandbox: a copy of a base sandbox you prepare once.

02The code

Install with pip install kosa8 trl datasets and set KOSA8_API_KEY. Then create a base sandbox, wrap it with environment_factory, and pass the result and reward_func to the trainer:

cloud = kosa8.Cloud(), then base = cloud.sandboxes.create(template="python"), then Env = environment_factory(cloud, base, grade_cmd=["python", "/grader/test.py"]), then GRPOTrainer(model=..., train_dataset=ds, reward_funcs=reward_func, environment_factory=Env).

Each dataset row can carry a files column with hidden grader files. They are used only when the episode is graded and are never shown to the model. The full example is examples/rl/grpo_trl.py in the kosa8 Python SDK, and wrapping it in kosa8.rl.Run labels, leases and cleans up every sandbox the run makes.

03What kosa8 adds to the loop

The model's commands run in a sandbox on kosa8 Cloud, not on the machine holding your weights and data. When an episode ends, reward_func grades it in a sealed copy with no network, and the grader's last output line gives the reward.

  • Its own sandbox per generation. One episode cannot see or change another.
  • Failures are masked, not scored. If the sandbox itself fails, the reward is None, which TRL leaves out of the loss instead of teaching the model that a crash is worth 0.
  • Cleanup is automatic. An episode's sandbox is deleted when it is graded, on the next reset, and at exit.
  • A record. Sandbox creates, commands and deletes are written to your organization's audit log.

04Limits to know

The adapter needs kosa8 Cloud and an API key; it does not run on a local kosa8 install. Plan limits on how many sandboxes run at once apply, so set num_generations and batch size to fit your plan. Other RL frameworks have adapters in the same module: Gymnasium, OpenEnv, Prime Intellect verifiers and SWE-bench.

Hugging Face is a trademark of its owner; kosa8 is not affiliated with or endorsed by Hugging Face.

Takeaways

  1. TRL's GRPOTrainer accepts an environment_factory; kosa8.rl.trl builds one backed by kosa8 Cloud sandboxes.
  2. Each generation acts in its own sandbox with bash and write_file tools.
  3. Rewards come from a sealed grader with no network; hidden test files never reach the model.
  4. Sandbox failures return None, so TRL leaves them out of the loss.

Where kosa8 fits · Zero Trust cybersecurity infrastructure for AI agents

kosa8 is Zero Trust cybersecurity infrastructure for AI agents. For RL it gives every rollout its own sandbox, grades it in a sealed copy, masks infrastructure failures instead of scoring them, and records what ran.

Read next: AI security for the enterprise · AI for cybersecurity · Cybersecurity for AI agents, topic by topic

Check yourself · +100 XP

What reward does the kosa8 TRL adapter return when the sandbox itself fails?

Questions people ask

Does this need a GPU on kosa8?

No. The model trains wherever you run TRL. kosa8 only runs the sandboxes the model's tool calls act in.

Is kosa8 a Hugging Face partner?

No. kosa8 ships an adapter that follows TRL's public environment_factory interface. Hugging Face is a trademark of its owner; kosa8 is not affiliated with or endorsed by Hugging Face.

Sources

  1. TRL documentation — Hugging Face
  2. RL training on kosa8 Cloud — kosa8 docs

Written by the kosa8 team. Every kosa8 command was checked against the CLI, and every claim about another product links to that product's own documentation. Spotted a mistake? Tell us.

Try it on your own machine

Give your next agent its own sealed machine.

One line installs kosa8 on your own machine. Start free with no card; plans from $29 a month.