Skip to main content

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_KEY and DECLAW_DOMAIN set in your environment
  • ANTHROPIC_API_KEY set 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)

The Anthropic Messages API returns a list of content blocks. For text output, access message.content[0].text.

Prompt engineering for code-only output

The system prompt tells Claude to respond only with Python code. Two keys:
  1. State ONLY with Python code explicitly
  2. Add No markdown fences, no explanation — Claude sometimes wraps code in ```python blocks regardless
Always call 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.