Notebooks
Notebook code can use Budget AI like any other application. When the notebook environment itself points AI calls at the Budget AI gateway, every notebook on a covered cluster, instance or image gets the same models, budgets, limits and guardrails, and people do not have to change their code.
There are two ways to set this up:
| Way | Use it when |
|---|---|
| Notebook setup kit | You run Databricks clusters, SageMaker notebook instances or Jupyter containers and want notebooks that call OpenAI() routed through the gateway with no code change. |
| Manual setup | You prefer to set the environment variables yourself, or you also want the ANTS Platform SDK in every kernel. |
Notebook setup kit
The kit sets up the notebook environment so that the openai Python library sends its calls to Budget AI. A notebook that runs OpenAI() with no arguments uses the gateway and a Budget AI key instead of calling the provider directly.
How it works
The openai library reads OPENAI_BASE_URL and OPENAI_API_KEY from the environment when it is given no arguments. The kit installs a small start-up hook into the environment's Python. Every time Python starts, in notebook kernels, scripts and batch jobs alike, the hook sets:
| Variable | Set to |
|---|---|
OPENAI_BASE_URL and OPENAI_API_BASE | Your Budget AI address |
OPENAI_API_KEY | Your Budget AI key |
The gateway does the enforcement: the key's model groups, expiry, rate limits and budget, and Budget AI guardrails when they are on. The ANTS Platform SDK is not needed.
Get the kit
The kit has four files:
| File | Use |
|---|---|
ants_notebook_bootstrap.py | The start-up hook and its installer. Standard library only, Python 3.8 or later. |
databricks/ants-gateway-init.sh | Databricks cluster init script |
sagemaker/ants-gateway-on-start.sh | SageMaker notebook instance on-start lifecycle script |
jupyter/Dockerfile | Jupyter image based on the Jupyter Docker Stacks |
Ask your ANTS team for the notebook setup kit.
Settings
Each environment needs two settings:
| Setting | Value |
|---|---|
ANTS_GATEWAY_BASE_URL | Your Budget AI address, shown in Project Settings > Budget AI > Budget AI address. /v1 is added when the address has no path. |
ANTS_GATEWAY_KEY | A Budget AI key, starting with sk-ants-. Keep it in your platform's secret store. |
The hook reads them from the environment first, then from the settings file /etc/ants-notebook/gateway.env. The file holds KEY=VALUE lines. The setup scripts write it, because a kernel only sees the environment its parent process started with, and a file reaches every kernel.
The Databricks and SageMaker scripts run the bootstrap file as root. Set ANTS_BOOTSTRAP_SHA256 to the SHA-256 of ants_notebook_bootstrap.py (sha256sum ants_notebook_bootstrap.py) and the script refuses to run a downloaded file that differs, and stops the start. It is optional, and recommended whenever the file comes from a URL or a bucket that other people can write to.
Set up each platform
The init script runs on every node when the cluster starts, the driver and the workers, so code that runs on workers is covered too.
Store the key
Put the Budget AI key in a Databricks secret scope, for example scope ants and key gateway-key.
Upload the files
Put ants-gateway-init.sh and ants_notebook_bootstrap.py where the cluster can read them, for example a Unity Catalog volume or workspace files.
Configure the cluster
Add ants-gateway-init.sh as a cluster init script. In Advanced options > Spark > Environment variables, set:
ANTS_BOOTSTRAP_SOURCE can also be an https:// URL. Optional: ANTS_BOOTSTRAP_SHA256, and ANTS_PYTHON_BIN if the cluster Python is not at /databricks/python/bin/python.
Start and check
Start the cluster, open a notebook and run the check in a %sh cell.
Init scripts run only when the cluster starts, so a new key or address needs a restart. Serverless compute and some access modes do not run init scripts; use a classic cluster that allows them.
Roll the kit out on a test cluster or instance first, and run the check there before you attach it to every environment.
Check that it works
From a notebook terminal or a %sh cell, run:
Only the last four characters of the key are shown. Exit code 0 means healthy, 1 a configuration problem (including "not installed for this Python") and 2 that the gateway check failed.
A notebook then calls the gateway with no extra setup:
Use a model group the key is granted as model. See Getting Started with Budget AI.
Behaviour
- Personal OpenAI settings are replaced. If a person has their own
OPENAI_API_KEYorOPENAI_BASE_URLset, the hook overwrites it, because leaving it would bypass the gateway. To keep values that are already set, setANTS_GATEWAY_RESPECT_EXISTING_OPENAI=true. - Setup fails closed. If a setting is missing or malformed, the init script or lifecycle script fails and the cluster or instance does not start, so a mistake cannot leave notebooks quietly uncovered. To start anyway with a warning, set
ANTS_SETUP_FAIL_OPEN=true. - Python always starts. The hook never stops Python from starting and never prints the key. If only one of the two settings is present, it prints a warning that AI calls are not being routed. On a machine with no settings it does nothing.
- The settings file is readable by all users on the node. Anyone on the cluster or instance can read the key, just as they could read it from the environment. Use one environment per team.
- One shared key per environment. Everyone on a cluster, instance or image deployment shares the key's budget and limits, and usage is attributed to that key. When the key expires, is blocked or runs out of budget, every notebook on the environment stops at once. Before the key expires, rotate it, update the secret and restart the environment.
- The SDK is optional. The kit does not install the ANTS Platform SDK unless you ask:
ANTS_INSTALL_SDK=truefor the Databricks and SageMaker scripts, or the build argumentWITH_SDK=truefor the Jupyter image. A failed SDK install prints a warning and does not stop the start, because the routing is already applied. If you install it, setANTS_PLATFORM_HOSTand the project key pair as in Manual setup. - Turn it off for one process.
ANTS_GATEWAY_DISABLED=truestops the hook for that process, for debugging. - The key goes to any OpenAI-compatible endpoint. The
openailibrary sendsOPENAI_API_KEYto whateverbase_urlit is given, so a notebook that calls another OpenAI-compatible provider sends it the Budget AI key. Give each environment its own key with a short expiry, only the model groups the notebooks need, and allowed source networks set to the environment's outbound addresses, so a copied key is refused anywhere else.
What the gateway serves
The gateway serves chat completions (POST /v1/chat/completions), embeddings (POST /v1/embeddings) and the model list (GET /v1/models).
| Client or call | Result with the kit |
|---|---|
openai library chat completions, embeddings and models | Routed through the gateway |
LangChain ChatOpenAI and OpenAIEmbeddings | Routed, as they read the same variables |
| Other OpenAI endpoints, such as responses, images, moderations, audio and files | Sent to the gateway, which does not serve them: 404 |
| Anthropic SDK | Calls Anthropic directly, not the gateway |
Amazon Bedrock through boto3 | Calls AWS directly, not the gateway |
AzureOpenAI client | Ignores the variables and calls Azure directly |
A call that goes directly to a provider succeeds with no budget, limit or guardrail. No kit setting changes this; use a network rule.
Make it enforcing
The kit makes the gateway the default route. It does not stop a notebook from calling a provider directly, for example with code that passes its own base_url or api_key, with the Anthropic, Bedrock or Azure OpenAI clients, with python -S (which skips the start-up hook), or with ANTS_GATEWAY_DISABLED or ANTS_GATEWAY_BASE_URL set by the user before Python starts.
To close these, add a network rule outside the notebook: allow the environment to reach the gateway, and block direct calls to api.openai.com, api.anthropic.com, the Bedrock runtime endpoints and the Azure OpenAI endpoints. Use a security group, an egress firewall, Databricks workspace network rules, or a SageMaker VPC without direct internet access. A bypass then fails instead of skipping the gateway.
What the kit does not cover
- Only OpenAI-compatible chat completions, embeddings and model listing go through the gateway. See What the gateway serves.
- It routes by default, it does not block. See Make it enforcing. Keep provider keys out of notebook environments so the gateway is the only route.
- Each Python needs its own install. A virtual environment that someone creates inside a notebook is not covered until the hook is installed into it.
- Other languages and hosted notebooks. R, Kaggle, Databricks serverless compute and SageMaker Studio are not covered. Google Colab is not covered by the kit; see Google Colab for a manual cell.
Manual setup
You can also set the variables yourself. Install the ants-platform Python SDK if you want SDK tracing and guardrail checks, and set these environment variables for every notebook kernel:
| Variable | Value | Used by |
|---|---|---|
OPENAI_BASE_URL | Your Budget AI address, ending in /v1 | OpenAI clients and libraries that read it. AI calls go through the gateway. |
OPENAI_API_KEY | A Budget AI key | The gateway, which applies the key's models, budgets and limits |
ANTS_PLATFORM_HOST | The URL of your ANTS Platform app | The SDK, for tracing and guardrail checks |
ANTS_PLATFORM_PUBLIC_KEY, ANTS_PLATFORM_SECRET_KEY | A project API key pair | The SDK |
The Budget AI address is shown in Project Settings > Budget AI > Budget AI address. See Getting Started with Budget AI.
Keep keys in your platform's secret store, not in the script or image. Usage through the gateway is attributed to the Budget AI key, so issue one key per cluster or instance, or per person where each person has their own notebook instance.
Store the keys
Put the Budget AI key and the project key pair in a Databricks secret scope.
Install the SDK
Add ants-platform as a cluster library, or install it with your own init script.
Set the environment variables
In Advanced options > Spark > Environment variables, set the variables above. Reference secrets as {{secrets/<scope>/<key>}}.
Enforce it for every cluster
Add the library and the variables to a cluster policy and require that policy, so every cluster created under it is covered.
Google Colab
Colab runtimes are created by Google for each user, so there is no init script, lifecycle configuration or image for the kit to install into. Each person can add a cell instead.
Store the Budget AI key as a Colab secret (the key icon in the left bar) named ANTS_GATEWAY_KEY, and allow notebook access to it. Run this cell before any AI call:
Create the OpenAI() client after the cell runs, because the library reads both variables when the client is created. The same gateway routes apply.
- The cell is opt-in. Nothing runs it for anyone, so coverage depends on each person adding it.
- It can be bypassed: a person can skip the cell, pass their own
base_urlandapi_key, or call a provider directly. There is no network rule you can apply to a Colab runtime. - The person running the notebook can read the key. Use a key with a short expiry and only the model groups needed. Colab's outbound addresses are not fixed, so allowed source networks are not practical there.
What is covered
- Models, budgets and limits. Calls through the gateway use only the model groups the key grants and count against its budget and rate limits.
- Guardrails. With Budget AI guardrails on, gateway traffic is checked against your guardrail policies with no notebook code change. Code can also use the SDK's guarded clients directly. See AI Guardrails.
- Usage reporting. Gateway calls appear in Budget AI observability and as traces.
Limits.
- Code that passes its own base URL, or calls a provider's SDK with a provider key, does not go through the gateway. Keep provider keys out of notebook environments so the gateway is the only route.
- Traces from the SDK are sent in the background. Notebooks that trace with the SDK must call
ants.flush()before the kernel stops. See Debugging. - Google Colab and other hosted notebooks need the variables set by hand in each notebook. See Google Colab.