This example demonstrates how to run a coding agent powered by Fireworks AI open models (Kimi, DeepSeek, Qwen, GLM, ...) inside a Daytona sandbox, with the Fireworks API key handled as a BYOK (bring your own key) secret that never enters the sandbox in plaintext.
The agent receives a small coding task (tests.py), iterates with write_file / run_bash tools until the tests pass, and exports its full trajectory in the OpenAI messages format — directly usable as a Fireworks supervised fine-tuning dataset.
- BYOK via Daytona secrets: The Fireworks key is stored as an organization secret with a host allowlist (
api.fireworks.ai). The sandbox only ever holds an opaque placeholder; Daytona's outbound proxy substitutes the real key at request time, and response scrubbing prevents sandbox code from ever reading it - Open-model inference: The agent calls Fireworks' OpenAI-compatible serverless API from inside the sandbox — no code changes versus any other OpenAI-compatible provider
- Safe code execution: Everything the model writes and runs executes in an isolated Daytona sandbox, not on your machine
- Verifiable outcome: The task is graded by
python tests.py— the same pass/fail signal a verifier produces in reinforcement learning - Fine-tuning data export: The full multi-turn trajectory (system/user/assistant/tool messages, including
tool_calls) is saved astrajectory.jsonl, ready forfirectl dataset create
- Python: Version 3.10 or higher
Tip
It's recommended to use a virtual environment (venv or poetry) to isolate project dependencies.
DAYTONA_API_KEY: Required for access to Daytona sandboxes. Get it from the Daytona DashboardFIREWORKS_API_KEY: Required. Get it from the Fireworks Dashboard. Used once on the host to create/rotate the Daytona secret — it is never sent to the sandboxFIREWORKS_MODEL(optional): Any serverless model from the Fireworks model library. Defaults toaccounts/fireworks/models/kimi-k3
See the .env.example file for the exact structure. Copy .env.example to .env and fill in your API keys before running.
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # macOS/Linux; Windows PowerShell: .\venv\Scripts\Activate.ps1
-
Install dependencies:
pip install -e . -
Copy
.env.exampleto.envand add your API keys. -
Run the example:
python main.py
The host stores your Fireworks key as a Daytona organization secret with a host allowlist:
daytona.secret.create(CreateSecretParams(
name="fireworks-api-key",
value=fireworks_key,
hosts=["api.fireworks.ai"],
))When the sandbox is created, the secret is mounted as an environment variable. The variable holds an opaque placeholder (dtn_secret_...), not the key:
sandbox = daytona.create(CreateSandboxFromSnapshotParams(
language="python",
secrets={"FIREWORKS_API_KEY": "fireworks-api-key"},
))The script proves both halves of the model:
printenv FIREWORKS_API_KEYinside the sandbox prints the placeholder- an authenticated
curltohttps://api.fireworks.ai/inference/v1/modelsreturns200, proving the proxy substituted the real key (Fireworks would reject the placeholder with a401)
Because substitution only happens for api.fireworks.ai, agent code cannot leak the key to any other host — a request anywhere else carries the harmless placeholder. See the Secrets documentation for details.
The agent runs entirely inside the sandbox and talks to Fireworks through the standard OpenAI client:
client = OpenAI(
base_url="https://api.fireworks.ai/inference/v1",
api_key=os.environ["FIREWORKS_API_KEY"], # the placeholder
)It exposes two tools to the model — write_file and run_bash — and loops until the model stops calling tools or hits the turn limit. The task: implement solution.py so that python tests.py passes.
After the loop, the agent reruns tests.py as the final verdict and writes the complete message history to trajectory.jsonl:
{"messages": [{"role": "system", ...}, {"role": "assistant", "tool_calls": [...]}, {"role": "tool", ...}, ...]}The host downloads the file. Fireworks uses the same OpenAI-compatible format for supervised fine-tuning data and requires between 3 and 3 million examples per dataset, so concatenate a few verified runs and upload the result:
cat trajectory.jsonl >> trajectories.jsonl # append after each verified run
firectl dataset create my-agent-trajectories trajectories.jsonl # once it has 3+ rowsCollect many successful runs (vary the tasks), and you have an SFT dataset distilled from your own agent — the standard first step before reinforcement learning on the Fireworks Training API.
One caveat worth knowing before you scale this up: in this demo, tests.py doubles as the task spec the model is allowed to read, so a solution could in principle memorize the visible cases instead of generalizing. Production reward functions should also score held-out cases the policy never sees.
- Swap in your own task by replacing
tests.py— anything gradeable by exit code works - Scale out: create hundreds of sandboxes in parallel for batch data generation (see the RL guides for the pool pattern)
- Train on the verifiable signal: the
tests.pyexit code used here is exactly the reward a verifier computes in Fireworks' Agentic RL cookbook — see also Daytona's reinforcement learning guides