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 hereSimple 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 listUsing Shortcuts
%%ai claude
Create a class for managing a shopping cart%%ai gpt4o
Explain the GIL in PythonAvailable shortcuts:
| Shortcut | Expands To |
|---|---|
gpt4o | openai:gpt-4o |
gpt4o-mini | openai:gpt-4o-mini |
claude | anthropic:claude-3-5-sonnet-latest |
claude-haiku | anthropic:claude-3-5-haiku-latest |
gemini | google:gemini-2.0-flash |
bedrock-claude | bedrock:anthropic.claude-3-5-sonnet |
Output Formats
Use the -f flag to control rendering:
Code Format
%%ai claude -f code
Write a binary search implementationOutput appears as a syntax-highlighted, copyable code block.
Markdown Format
%%ai claude -f markdown
Explain the MVC pattern with diagramsOutput renders as formatted markdown with headers, lists, etc.
Text Format
%%ai claude -f text
Give me a one-line summary of machine learningPlain text output, no formatting.
HTML Format
%%ai claude -f html
Create a simple styled buttonRenders HTML directly in the output.
Math Format
%%ai claude -f math
Show the formula for standard deviationRenders LaTeX math notation.
JSON Format
%%ai claude -f json
Return a JSON object representing a user profilePretty-printed JSON output.
Image Format
For image generation models:
%%ai openai:dall-e-3 -f image
A serene mountain landscape at sunsetVariable 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 tasksExamples
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 timeDocumentation
%%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 codewhen you want copyable code output - Use
-f markdownfor explanations and documentation - Reference variables to provide context
- Be specific in your prompts for better results
- Combine cell references for debugging workflows