What You’ll Learn
- How to call the Anthropic Messages API and feed the response directly into a sandbox
- Structuring prompts to get code-only output (no markdown, no explanation)
- Sandbox-per-task isolation: fresh sandbox for each code generation request
- Demo mode: run the full workflow without an Anthropic API key
Prerequisites
- Declaw running locally or in the cloud (see Deployment)
DECLAW_API_KEYandDECLAW_DOMAINset in your environmentANTHROPIC_API_KEYset in your environment (optional — demo mode runs without it)
This example is available in Python. TypeScript support coming soon.
Code Walkthrough
Live mode (requires ANTHROPIC_API_KEY)
message.content[0].text.
Prompt engineering for code-only output
The system prompt tells Claude to respond only with Python code. Two keys:- State
ONLY with Python codeexplicitly - Add
No markdown fences, no explanation— Claude sometimes wraps code in```pythonblocks regardless
strip_code_fences() as a defensive measure even when the prompt says not to include fences.
Demo mode — text readability analysis
The demo mode runs a pre-written analysis script that computes Flesch Reading Ease for a sample text:Expected Output (demo mode)
Comparison with OpenAI Example
The pattern is identical to the OpenAI example — only the API client and model differ. This makes it easy to swap providers:
The sandbox creation and execution steps are the same regardless of which LLM you use.