AI·

Interactive Code Execution with Artifacts

Run Python code directly in blog posts using Anthropic Code Execution. Try the interactive examples below — every snippet runs in an isolated container on Anthropic infrastructure.
A glowing terminal window floating in mid-air inside a sleek, dark workspace, its screen displaying vibrant Python code alongside a colorful matplotlib chart rendering in real-time, with streams of luminous data particles flowing from the terminal into a translucent isolated container hovering beside it. Dramatic rim lighting in cool electric blue and warm amber picks out the edges of the floating elements against a deep charcoal background, with subtle bokeh circles suggesting depth. Cinematic photorealism, 8k quality, shallow depth of field with tack-sharp focus on the terminal screen, professional photography composition following rule of thirds. High contrast color grading with teal, orange, and violet accents, 16:9 landscape format with negative space on the left for potential text overlay.

I've been wanting a way to let readers run code directly in blog posts — not just read about it. Today I'm shipping interactive artifacts powered by Anthropic's Code Execution Tool.

Every code block below runs in an isolated Linux container on Anthropic's infrastructure. No local setup needed, no browser sandboxing tricks. Real Python, real output.

How It Works

The ::code-runner component sends your code (or a prompt) to Claude via the Messages API with the code_execution tool enabled. Claude runs it in a sandboxed container and streams back stdout, stderr, and any generated files.

The container persists between runs within the same artifact, so you can build up state across multiple executions.

Example 1: Fibonacci Sequence

A classic starting point. Hit "Run" to see the first 15 Fibonacci numbers.

Fibonacci Generatorpython

Example 2: Data Visualization

This one generates a matplotlib chart. The resulting PNG renders inline below the output.

Population Bar Chartpython
Create a horizontal bar chart showing estimated 2025 world population by continent. Use matplotlib with a dark background style. Save as population.png.

Example 3: Quick Math

Edit the code and re-run to experiment.

Prime Sievepython

Under the Hood

The artifact system uses three Anthropic beta APIs:

  • Code Execution (code-execution-2025-08-25) — runs code in isolated containers
  • Files API (files-api-2025-04-14) — downloads generated files (images, HTML, etc.)
  • Skills API (skills-2025-10-02) — optional document generation capabilities

Each execution streams results via SSE, so you see output as it happens rather than waiting for the full response.

The container has Python with common libraries pre-installed (numpy, pandas, matplotlib, etc.) but no internet access — it's fully sandboxed.

What's Next

This is the foundation for more interactive content. I'm exploring adding artifact execution to the chat interface and supporting more languages beyond Python.

If you have ideas for interactive examples you'd like to see, let me know on X.