Skip to main content

aimagics

Installation

Install latest from the GitHub repository:

$ pip install git+https://github.com/adrische/aimagics.git

or from conda

$ conda install -c adrische aimagics

or from pypi

$ pip install aimagics

Setup

1. Load the package

After installation, you can load the package in Jupyter and ipython with

%load_ext aimagics

Whether the package has been loaded can be checked with

from IPython import get_ipython
get_ipython().extension_manager.loaded
{'IPython.extensions.storemagic', 'aimagics'}

‘aimagics’ should appear in the output.

2. Set LLM API key

You should set your environment API key to your favorite LLM provider. The default model is

AIMagics().model
'openrouter/openai/gpt-oss-120b'

so the environment key needed is OPENROUTER_API_KEY.

All LiteLLM models are compatible.

3. Turn auto save on

The package works by retrieving the current notebook from disk. To always get the current state, it is recommended to turn auto save on. This is the default in jupyter notebooks, and can be toggled in vscode via Show and Run Commands > File: Toggle Auto Save.

Usage

The package exposes two commands: %ai and %%ai. These are so-called ‘line’ and ‘cell magics’ and can be used as follows:

Line magic

The command %ai processes what comes after on the same line as request to the LLM:

%ai What is aimagics?

aimagics is a Python package and IPython extension that brings Large Language Model (LLM) capabilities directly into Jupyter notebooks via magic commands.

Key Features:

  • Magic Commands: Offers %ai (line magic) and %%ai (cell magic) to interact with models directly within code cells.
  • Context-Aware: Reads the current notebook state from disk to provide context-aware responses to your code and markdown.
  • Broad Model Support: Built on LiteLLM, allowing you to connect to OpenRouter, OpenAI, Anthropic, and dozens of other LLM providers.

Cell magic

The command %%ai processes what comes after it on the same line, but also what is in the same cell below it:

%%ai Why does the following code fail?
1/0

Answer

1/0

fails because it raises a ZeroDivisionError. In Python (and mathematics), division by zero is undefined, so attempting to compute 1 / 0 triggers this exception:

ZeroDivisionError: division by zero

To avoid the error, ensure the denominator is never zero, e.g.:

denominator = 2  # any non‑zero value
result = 1 / denominator

By default, the entire notebook up to and including the calling cell is included in the prompt as context.

Configuration

The possible configuration options can be viewed with

%config AIMagics
AIMagics(Magics) options
----------------------
AIMagics.model=<Unicode>
    Provider/model to be used.
    Current: 'openrouter/openai/gpt-oss-120b'
AIMagics.system_prompt=<Unicode>
    The system prompt prepended to any prompt and context.
    Current: "You are a helpful assistant living inside a user's Jupyter notebook. \n        Use markdown syntax for styling your responses.\n        Keep your responses brief and to the point.\n"

For example, you can change the model with

%config AIMagics.model = "openrouter/google/gemini-3.8-flash"
%ai what model are you?

I am Gemini (specifically configured as openrouter/google/gemini-3.8-flash), a large language model trained by Google.

Documentation

Documentation can be found hosted on this GitHub repository’s pages. Additionally you can find package manager specific guidelines on conda and pypi respectively.

Acknowledgements

This repository would not be possible without the FastAI / AnswerAI open source packages, in particular FastLLM. AnswerAI even have a dedicated platform for notebooks with AI integration: SolveIt.

There are a number of packages implementing basically the same ideas (just much better):

During the finishing stages I also found https://pypi.org/project/aimagic/ on PyPi, which is basically what I am implementing here, even with the same syntax and the same name, just for Jupyter (relying on Javascript to get the cells for context).

Metadata

Release files for aimagics 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aimagics 0.0.1
File Size Uploaded
aimagics-0.0.1.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aimagics 0.0.1
File Interpreter ABI Platform
aimagics-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / aimagics-0.0.1.tar.gz

Download URL aimagics-0.0.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4792a810d4535cfce74f3b889384ad2ef08f65cc117a108a9ddb981f80509ee3
BLAKE2b-256 checksum
How to use checksums
cf98c4626331e17c4686679ee2c7cb129d2b9657cd7bb6ea958eac934bb67ef8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / aimagics-0.0.1-py3-none-any.whl

Download URL aimagics-0.0.1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb2a6b8a49e75721eb08839b1e44707d163f647f5acc4b1d93417d8d6e5e7bde
BLAKE2b-256 checksum
How to use checksums
81664c6beb806c02e7fb119809a9f2bf775ab126271091941349c088f705ba4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release history Release notifications | RSS feed

0.0.3

2 release files

0.0.2

2 release files

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page