← All tutorialsDevTools

Sandboxed code runner

Let users prompt "plot AAPL last 5 years". The agent writes Python, runs it in E2B, returns the chart. Per-user $0.50 cap.

Cost~$0.08 per run
Operations
code.executetext_generation.generate

Prerequisites

  • code.execute enabled (E2B-backed sandboxes).
  • Track per-user spend in your DB - set a $0.50 application-level cap before dispatching.

Walkthrough

1. Generate code

Ask GPT-4o for a Python snippet that uses yfinance + matplotlib and prints a base64 PNG.

2. Execute in E2B

Hand the code to code.execute. The sandbox runs for up to 60s, returns stdout + stderr.

bash# Run user-generated Python in an isolated sandbox with a 60s ceiling.
curl -X POST https://www.upivia.com/v1/service-requests \
  -H "Authorization: Bearer $AGENT_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "service":"code",
    "operation":"execute",
    "payload":{"language":"python","code":"<the generated snippet>"}
  }'

3. Return to user

Decode the base64 PNG from stdout, render in your UI. If stderr is non-empty, feed it back to GPT-4o and retry once.

Next steps

Audit every call at /audit-logs, watch spend at /usage, and tune budgets per service on the agent's page.

Create an account →