Documentation Index Fetch the complete documentation index at: https://docs.tokenlab.sh/llms.txt
Use this file to discover all available pages before exploring further.
エラー応答形式
すべてのエラーは、オプションの Agent-First hints を含む一貫した JSON 形式で返されます:
{
"error" : {
"message" : "Human-readable error description" ,
"type" : "error_type" ,
"code" : "error_code" ,
"param" : "parameter_name" ,
"did_you_mean" : "suggested_model" ,
"suggestions" : [{ "id" : "model-id" }],
"hint" : "Next step guidance" ,
"retryable" : true ,
"retry_after" : 30
}
}
基本フィールド(message, type, code, param)は常に含まれます。ヒント系フィールド(did_you_mean, suggestions, hint, retryable, retry_after, balance_usd, estimated_cost_usd)は、AIエージェントの自己修正のための任意の拡張です。詳細は Agent-First API guide を参照してください。
OpenAI互換のエンドポイントはTokenLabの安定したゲートウェイエラータイプを使用します。Anthropic互換およびGemini互換のエンドポイントは、それぞれネイティブのエラー系列とレスポンス形式を使用します。
HTTP ステータスコード
コード 説明 400 Bad Request - 無効なパラメータ 401 Unauthorized - 無効または欠落したAPIキー 402 Payment Required - 残高不足 403 Forbidden - アクセス拒否またはモデルが許可されていない 404 Not Found - モデルまたはリソースが見つからない 413 ペイロードが大きすぎます - 入力またはファイルサイズが超過 429 Too Many Requests - レート制限超過 500 Internal Server Error - 内部サーバーエラー 502 Bad Gateway - 上流プロバイダエラー 503 Service Unavailable - サービス一時的に利用不可 504 Gateway Timeout - リクエストがタイムアウト
エラータイプ
認証エラー (401)
タイプ コード 説明 invalid_api_keyinvalid_api_keyAPI key が見つからないか無効です expired_api_keyexpired_api_keyAPI key は取り消されています
from openai import OpenAI, AuthenticationError
try :
response = client.chat.completions.create( ... )
except AuthenticationError as e:
print ( f "Authentication failed: { e.message } " )
支払いエラー (402)
タイプ コード 説明 insufficient_balanceinsufficient_balanceアカウント残高が不足しています quota_exceededquota_exceededAPIキーの使用上限に達しました
from openai import OpenAI, APIStatusError
try :
response = client.chat.completions.create( ... )
except APIStatusError as e:
if e.status_code == 402 :
print ( "Please top up your account balance" )
アクセスエラー (403)
タイプ コード 説明 access_deniedaccess_deniedリソースへのアクセスが拒否されました access_deniedmodel_not_allowedこのAPIキーではモデルが許可されていません
{
"error" : {
"message" : "You don't have permission to access this model" ,
"type" : "access_denied" ,
"code" : "model_not_allowed"
}
}
バリデーションエラー (400)
タイプ 説明 invalid_request_errorリクエストパラメータが無効です context_length_exceededモデルに対して入力が長すぎます model_not_found要求したモデルは現在のパブリック契約で利用できません
{
"error" : {
"message" : "Model not found: please check the model name" ,
"type" : "invalid_request_error" ,
"param" : "model" ,
"code" : "model_not_found" ,
"did_you_mean" : "gpt-5.4" ,
"suggestions" : [{ "id" : "gpt-5.4" }, { "id" : "gpt-5-mini" }],
"hint" : "Did you mean 'gpt-5.4'? Use GET https://api.tokenlab.sh/v1/models to list all available models."
}
}
パブリックルートは、レスポンス本文でタイポ、非表示、保留、非公開のモデル状態を区別しません。モデルが現在パブリック契約を通じて利用できない場合、TokenLabはmodel_not_foundを返します。
レート制限エラー (429)
当該レート制限を超えた場合:
{
"error" : {
"message" : "Rate limit: 60 rpm exceeded" ,
"type" : "rate_limit_exceeded" ,
"code" : "rate_limit_exceeded" ,
"retryable" : true ,
"retry_after" : 8 ,
"hint" : "Rate limited. Retry after 8s. Current limit: 60/min for user role."
}
}
含まれるヘッダー:
Retry-Afterヘッダーとretry_afterフィールドは、いずれも再試行するまで待つ秒数を示します。
ペイロードが大きすぎる (413)
入力またはファイルサイズが制限を超えた場合:
{
"error" : {
"message" : "Input size exceeds maximum allowed" ,
"type" : "invalid_request_error" ,
"code" : "payload_too_large"
}
}
主な原因:
画像ファイルが大きすぎる(最大20MB)
音声ファイルが大きすぎる(最大25MB)
入力テキストがモデルのコンテキスト長を超えている
上流エラー (502, 503)
タイプ 説明 upstream_errorプロバイダがエラーを返しました all_channels_failed利用可能なプロバイダがありません timeout_errorリクエストがタイムアウトしました
すべてのチャネルが失敗した場合、レスポンスには代替モデルが含まれます:
{
"error" : {
"message" : "Model claude-opus-4-6 temporarily unavailable" ,
"code" : "all_channels_failed" ,
"retryable" : true ,
"retry_after" : 30 ,
"alternatives" : [
{ "id" : "claude-sonnet-4-6" , "status" : "available" , "tags" : []},
{ "id" : "gpt-5-mini" , "status" : "available" , "tags" : []}
],
"hint" : "Retry in 30s or switch to an available model."
}
}
Pythonでのエラー処理
from openai import OpenAI, APIError, RateLimitError, APIConnectionError
client = OpenAI(
api_key = "sk-your-api-key" ,
base_url = "https://api.tokenlab.sh/v1"
)
def chat_with_retry ( messages , max_retries = 3 ):
for attempt in range (max_retries):
try :
return client.chat.completions.create(
model = "gpt-4o" ,
messages = messages
)
except RateLimitError as e:
if attempt < max_retries - 1 :
import time
time.sleep( 2 ** attempt) # Exponential backoff
continue
raise
except APIConnectionError as e:
print ( f "Connection error: { e } " )
raise
except APIError as e:
print ( f "API error: { e.status_code } - { e.message } " )
raise
JavaScriptでのエラー処理
import OpenAI from 'openai' ;
const client = new OpenAI ({
apiKey: 'sk-your-api-key' ,
baseURL: 'https://api.tokenlab.sh/v1'
});
async function chatWithRetry ( messages , maxRetries = 3 ) {
for ( let attempt = 0 ; attempt < maxRetries ; attempt ++ ) {
try {
return await client . chat . completions . create ({
model: 'gpt-4o' ,
messages
});
} catch ( error ) {
if ( error instanceof OpenAI . RateLimitError ) {
if ( attempt < maxRetries - 1 ) {
await new Promise ( r => setTimeout ( r , 2 ** attempt * 1000 ));
continue ;
}
}
throw error ;
}
}
}
ベストプラクティス
レート制限にかかった場合、再試行の間隔を徐々に長くしてください: wait_time = 2 ** attempt # 1s, 2s, 4s, 8s...
ハングするリクエストを避けるために、常に適切なタイムアウトを設定してください: client = OpenAI( timeout = 60.0 ) # 60 second timeout
サポートのためにリクエストIDを含む完全なエラー応答をログに記録してください: except APIError as e:
logger.error( f "API Error: { e.status_code } - { e.message } " )
一部のモデルには特定の要件(例:最大トークン数、画像フォーマット)があります。
リクエストを行う前に入力を検証してください。