Use Cases
Coding Agents
Build AI coding agents with Vibengine sandboxes
Coding Agents
Build AI-powered coding agents that can write, execute, and iteratively debug code inside isolated Vibengine sandboxes.
Architecture
A coding agent combines an LLM with a Vibengine sandbox to create an autonomous code-writing loop:
- LLM — Generates code based on the user prompt
- Vibengine Sandbox — Executes the code in an isolated environment
- Feedback Loop — The agent reads stdout/stderr, detects errors, and asks the LLM to fix them
This loop continues until the code runs successfully or a maximum retry count is reached.
Implementation
1. Create a Sandbox
import { Sandbox } from "vibengine"
const sandbox = await Sandbox.create({
timeoutMs: 5 * 60 * 1000, // 5 minutes for agent sessions
})from vibengine import Sandbox
sandbox = Sandbox.create(timeout=300) # 5 minutes2. Define Tools for Code Execution
Give your LLM a tool that runs code inside the sandbox and returns the output.
async function executeCode(sandbox, code) {
const result = await sandbox.runCode(code)
return {
stdout: result.stdout,
stderr: result.stderr,
exitCode: result.exitCode,
}
}def execute_code(sandbox, code: str) -> dict:
result = sandbox.run_code(code)
return {
"stdout": result.stdout,
"stderr": result.stderr,
"exit_code": result.exit_code,
}3. Build the Agent Loop
The agent writes code, runs it, observes the output, and retries on failure.
import { Sandbox } from "vibengine"
async function codingAgent(task, llm) {
const sandbox = await Sandbox.create({ timeoutMs: 5 * 60 * 1000 })
let attempts = 0
const maxAttempts = 5
let lastError = null
while (attempts < maxAttempts) {
const prompt = lastError
? `The code failed with this error:\n${lastError}\nFix the code and try again.`
: `Write code to accomplish this task: ${task}`
const code = await llm.generate(prompt)
const result = await sandbox.runCode(code)
if (result.exitCode === 0) {
console.log("Success:", result.stdout)
await sandbox.kill()
return result.stdout
}
lastError = result.stderr || result.stdout
attempts++
console.log(`Attempt ${attempts} failed, retrying...`)
}
await sandbox.kill()
throw new Error(`Agent failed after ${maxAttempts} attempts`)
}from vibengine import Sandbox
def coding_agent(task: str, llm) -> str:
sandbox = Sandbox.create(timeout=300)
attempts = 0
max_attempts = 5
last_error = None
while attempts < max_attempts:
if last_error:
prompt = f"The code failed with this error:\n{last_error}\nFix the code and try again."
else:
prompt = f"Write code to accomplish this task: {task}"
code = llm.generate(prompt)
result = sandbox.run_code(code)
if result.exit_code == 0:
print("Success:", result.stdout)
sandbox.kill()
return result.stdout
last_error = result.stderr or result.stdout
attempts += 1
print(f"Attempt {attempts} failed, retrying...")
sandbox.kill()
raise Exception(f"Agent failed after {max_attempts} attempts")Tips
Use long timeouts — Agent sessions can take several minutes as the LLM iterates. Set timeoutMs to at least 5 minutes.
- Pre-install packages in templates — Create a custom template with common libraries (numpy, pandas, requests) so your agent does not waste time installing them on every run.
- Capture both stdout and stderr — Always check both streams. Some libraries log important information to stderr even on success.
- Limit retries — Set a maximum attempt count to avoid infinite loops and runaway costs.
- Use filesystem tools — Let the agent write files to the sandbox filesystem for multi-file projects, not just inline code execution.