What You’ll Learn
- The fundamental pattern: LLM on host, code execution in sandbox
- How to strip markdown code fences from LLM output before executing
- How to create and destroy a fresh sandbox per task for strong isolation
- Demo mode: run the full workflow without an OpenAI API key
Prerequisites
- Declaw running locally or in the cloud (see Deployment)
DECLAW_API_KEY and DECLAW_DOMAIN set in your environment
OPENAI_API_KEY set in your environment (optional — demo mode runs without it)
This example is available in Python. TypeScript support coming soon.
Code Walkthrough
Architecture
The LLM never runs inside the sandbox. Only the generated code does. This ensures that even if the LLM produces malicious code, it executes in an isolated sandbox with no access to host resources.
Live mode (requires OPENAI_API_KEY)
Stripping code fences
LLMs often wrap code in markdown fences even when instructed not to. Always strip them before executing:
Demo mode (no API key required)
The example ships with a demo mode that uses hardcoded “LLM output” so you can verify the sandbox execution path without an API key:
Mode selection
The example auto-detects whether to run live or demo:
Expected Output (demo mode)
Security Note
A fresh sandbox is created for each task in this example. This is intentional: it ensures that code from one task cannot read files or environment variables left over from a previous task. For long-running sessions where state should persist across tasks, reuse the same sandbox — but understand that state accumulates.