InsufficientCreditsError
together_ai.errors.InsufficientCreditsError
Stack trace
together_ai.errors.InsufficientCreditsError: Insufficient credits available to complete the request. Please top up your account or reduce usage.
Why it happens
Together AI enforces credit-based usage limits. When your account balance is too low to cover the cost of the API call, the SDK raises InsufficientCreditsError to prevent overuse. This protects both the user and the service from unexpected charges or abuse.
Detection
Monitor API responses for InsufficientCreditsError exceptions and log account credit balances regularly to detect low credit states before calls fail.
Causes & fixes
Account credit balance is zero or below the required amount for the requested API call.
Add credits to your Together AI account via the dashboard or billing portal to restore API access.
Incorrect or missing API key causing the system to treat the request as unauthorized or unpaid.
Verify that your API key is correctly set in the environment variable and passed to the Together AI client.
Excessive API usage exceeding your current credit limit within a billing period.
Implement usage throttling or upgrade your plan to increase your credit limit.
Code: broken vs fixed
import os
from together_ai import TogetherAIClient
client = TogetherAIClient(api_key=os.environ['TOGETHER_API_KEY'])
response = client.chat_completions.create(model='together-large', messages=[{'role':'user','content':'Hello'}]) # Raises InsufficientCreditsError here import os
from together_ai import TogetherAIClient, InsufficientCreditsError
client = TogetherAIClient(api_key=os.environ['TOGETHER_API_KEY'])
try:
response = client.chat_completions.create(model='together-large', messages=[{'role':'user','content':'Hello'}])
print(response)
except InsufficientCreditsError:
print('Error: Insufficient credits. Please top up your Together AI account.') # Added error handling for insufficient credits Workaround
Catch InsufficientCreditsError in your code and queue or delay requests until credits are replenished to avoid immediate failure.
Prevention
Implement credit balance monitoring and usage limits in your application to proactively manage credit consumption and avoid hitting zero balance.