Error handling
Map code on the JSON body to a typed SDK error.
| code | meaning |
|---|---|
seller_unreachable | Winning seller timed out or 5xx |
bid_timeout | No match before the wait expired |
rate_limited | Buyer key exceeded burst |
insufficient_balance | Ledger rejected the trade |
use token_gateway_sdk::{BuyerChatMessage, BuyerClient, BuyerKey, ChatCompletionsRequest, SdkError};
let client = BuyerClient::new("http://127.0.0.1:8080", BuyerKey("sk-buyer".into()));
match client
.chat_completions(&ChatCompletionsRequest {
model: "gpt-4".into(),
messages: vec![BuyerChatMessage {
role: "user".into(),
content: "hello".into(),
}],
stream: false,
max_price: None,
temperature: None,
max_tokens: None,
})
.await
{
Ok(_) => {}
Err(SdkError::Gateway { code, .. }) => {
let _ = code;
}
Err(_) => {}
}from token_gateway import BuyerAuth, BuyerTokenGateway, NoMatchError, RateLimitError
gw = BuyerTokenGateway("http://127.0.0.1:8080", buyer=BuyerAuth("sk-buyer"))
try:
gw.chat_completions(model="gpt-4", messages=[{"role": "user", "content": "hello"}], max_price="0.05")
except NoMatchError as exc:
print(exc.code)
except RateLimitError as exc:
print(exc.code)import { BuyerClient, NoMatchError, RateLimitError } from "@ai-token-gateway/sdk";
const buyer = new BuyerClient({ baseUrl: "http://127.0.0.1:8080", apiKey: "sk-buyer" });
try {
await buyer.chatCompletions({
model: "gpt-4",
messages: [{ role: "user", content: "hello" }],
max_price: "0.05",
});
} catch (err) {
if (err instanceof NoMatchError || err instanceof RateLimitError) {
console.log(err.code);
}
}