Cell Magic

The %%ai cell magic sends your entire cell content as a prompt to an AI model and displays the response.

Basic Syntax

%%ai [provider:model] [-f format]
Your prompt here

Simple Usage

Without specifying a model, uses your default:

%%ai
What is the difference between a list and a tuple in Python?

Specifying Models

Provider:Model Syntax

%%ai openai:gpt-4o
Explain recursion with a simple example
%%ai anthropic:claude-3-5-sonnet-latest
Write a function to reverse a linked list

Using Shortcuts

%%ai claude
Create a class for managing a shopping cart
%%ai gpt4o
Explain the GIL in Python

Available shortcuts:

ShortcutExpands To
gpt4oopenai:gpt-4o
gpt4o-miniopenai:gpt-4o-mini
claudeanthropic:claude-3-5-sonnet-latest
claude-haikuanthropic:claude-3-5-haiku-latest
geminigoogle:gemini-2.0-flash
bedrock-claudebedrock:anthropic.claude-3-5-sonnet

Output Formats

Use the -f flag to control rendering:

Code Format

%%ai claude -f code
Write a binary search implementation

Output appears as a syntax-highlighted, copyable code block.

Markdown Format

%%ai claude -f markdown
Explain the MVC pattern with diagrams

Output renders as formatted markdown with headers, lists, etc.

Text Format

%%ai claude -f text
Give me a one-line summary of machine learning

Plain text output, no formatting.

HTML Format

%%ai claude -f html
Create a simple styled button

Renders HTML directly in the output.

Math Format

%%ai claude -f math
Show the formula for standard deviation

Renders LaTeX math notation.

JSON Format

%%ai claude -f json
Return a JSON object representing a user profile

Pretty-printed JSON output.

Image Format

For image generation models:

%%ai openai:dall-e-3 -f image
A serene mountain landscape at sunset

Variable Interpolation

Reference Python variables in your prompts:

code = """
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
"""
%%ai claude
Explain this code and suggest optimizations:
{code}

Cell References

Reference other cells:

%%ai claude
Explain what cell 5 does: {In[5]}
%%ai claude
What does this output mean: {Out[3]}
%%ai claude
Fix this error: {Err[7]}

Multi-line Prompts

Your entire cell content becomes the prompt:

%%ai claude -f code
Create a Python class that:
1. Manages a list of tasks
2. Supports adding and removing tasks
3. Can mark tasks as complete
4. Has a method to list all incomplete tasks

Examples

Code Explanation

%%ai claude
What does this regex do: ^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$

Code Generation

%%ai claude -f code
Write a decorator that logs function execution time

Documentation

%%ai claude -f markdown
Document this function with docstring and usage examples:
{In[10]}

Debugging

%%ai claude
I'm getting this error: {Err[5]}
The code that caused it: {In[5]}
How do I fix it?

Translation

%%ai claude -f code
Convert this JavaScript to Python:

function quickSort(arr) {
    if (arr.length <= 1) return arr;
    const pivot = arr[0];
    const left = arr.slice(1).filter(x => x < pivot);
    const right = arr.slice(1).filter(x => x >= pivot);
    return [...quickSort(left), pivot, ...quickSort(right)];
}

Tips

  • Use -f code when you want copyable code output
  • Use -f markdown for explanations and documentation
  • Reference variables to provide context
  • Be specific in your prompts for better results
  • Combine cell references for debugging workflows