While working on the “Building with the Claude API” course from Anthropic.
I created a jupyter notebook on Anitgravity ran the following code :
import os
from dotenv import load_dotenv
import anthropic
# 1. Load the environment variables from the .env file, allowing overrides
load_dotenv(override=True)
# 2. Create Anthropic Client
client = anthropic.Anthropic()
# 3. Specify the model
model = "claude-haiku-4-5-20251001"
# 4. Send the message request
message = client.messages.create(
model=model,
max_tokens=1000,
system="You are a helpful and precise assistant.",
messages=[
{
"role": "user",
"content": "What is quantum computing? Answer in one sentence"
}
]
)
# Print the response text
print(message.content[0].text)
And I got the following “Insufficient Balance” error as below:
APIStatusError: Error code: 402 - {'error': {'message': 'Insufficient Balance', 'type': 'unknown_error', 'param': None, 'code': 'invalid_request_error'}} Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
Troubleshooting steps:
Used curl cli to verify my Athropic API (around $25 fund in my Anthropic account) that have sufficient funds in it.
curl https://api.anthropic.com/v1/messages \
--header "x-api-key: YOUR_API_KEY_HERE" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data '{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 10,
"messages": [{"role": "user", "content": "Respond with the word OK."}]
}'
It returned “OK” as response message below:
{"model":"claude-haiku-4-5-20251001","id":"msg_01D7CxRLpfey7jHgTscbdP6o","type":"message","role":"assistant","content":[{"type":"text","text":"OK"}],"stop_reason":"end_turn","stop_sequence":null,"stop_details":null,"usage":{"input_tokens":14,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":4,"service_tier":"standard","inference_geo":"not_available"}}
This means that my API key is active and there are sufficient fund in my Anthropic account.
After a checking with “Gemini 3.5” in “Antigravity” using the following prompt :
How to troubleshoot load_dotenv() where using curl command cli is able to call claude api successfully but getting error 'insufficient fund" when running the python code in jupyther notebook?
It found that my system has a global environment variable set: ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
which was set previous for my previous learning/testing that causing this error.
The resolution is to explicitly specify “ANTHROPIC_BASE_URL” in the code to point it to “https://api.anthropic.com”” as updated code below:
import os
from dotenv import load_dotenv
import anthropic
# 1. Load the environment variables from the .env file, allowing overrides
load_dotenv(override=True)
# 2. Explicitly bypass the system ANTHROPIC_BASE_URL environment variable
# (which is routing requests to deepseek.com on your system)
client = anthropic.Anthropic(base_url="https://api.anthropic.com")
# 3. Specify the model you verified with curl
model = "claude-haiku-4-5-20251001"
# 4. Send the message request
message = client.messages.create(
model=model,
max_tokens=1000,
system="You are a helpful and precise assistant.",
messages=[
{
"role": "user",
"content": "What is quantum computing? Answer in one sentence"
}
]
)
# Print the response text
print(message.content[0].text)
And the code above successfully returned the response from Anthropic :
Quantum computing uses quantum bits (qubits) that can exist in multiple states simultaneously, allowing quantum computers to process certain types of problems much faster than classical computers.
Please let me know in the comment section if you also facing similar error and how did you resolve it. Thanks and have a nice day.