What You’ll Learn
- Sending prompts to the Anthropic Messages API (
claude-sonnet-4-20250514) to generate executable Python code - Stripping markdown code fences from LLM responses before execution
- Writing generated code into a Declaw sandbox filesystem
- Executing the code securely with
sbx.commands.run() - Graceful demo mode when no API key is configured
Prerequisites
- Declaw instance running and
DECLAW_API_KEY/DECLAW_DOMAINset ANTHROPIC_API_KEY(optional — the example runs in demo mode without it)
- Python
- TypeScript
Code Walkthrough
- Python
- TypeScript
Expected Output
In demo mode (no
ANTHROPIC_API_KEY), the output shows pre-written code results: the first 15 Fibonacci numbers and the sum of integers 1–100 (5050).