Configuration
Updated
The Chaos client accepts a ChaosSDKOptions object for configuration.
ChaosSDKOptions
interface ChaosSDKOptions {
apiKey: string;
baseUrl?: string;
timeout?: number;
useNativeHttp?: boolean;
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | — | Your Chaos Labs API key (starts with ck-) |
baseUrl | string | No | 'https://ai.chaoslabs.co' | Base URL for the Chaos AI API |
timeout | number | No | 120000 (2 min) | Request timeout in milliseconds |
useNativeHttp | boolean | No | undefined | Use native HTTP client instead of fetch |
Basic Configuration
import { Chaos } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({
apiKey: process.env.CHAOS_API_KEY!,
});
// Accessors
console.log(chaos.apiKey); // 'ck-...'
console.log(chaos.baseUrl); // 'https://ai.chaoslabs.co'Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Custom Base URL
Override the base URL for staging or self-hosted environments:
import { Chaos } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({
apiKey: process.env.CHAOS_API_KEY!,
baseUrl: 'https://staging.ai.chaoslabs.co',
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Custom Timeout
Some queries (complex portfolio analysis, multi-step transactions) may take longer than the default 2-minute timeout:
import { Chaos } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({
apiKey: process.env.CHAOS_API_KEY!,
timeout: 300000, // 5 minutes
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Error Handling
The SDK provides two error classes:
import { Chaos, WALLET_MODEL, ChaosError, ChaosTimeoutError } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({ apiKey: process.env.CHAOS_API_KEY! });
try {
const response = await chaos.chat.responses.create({
model: WALLET_MODEL,
input: [{ type: 'message', role: 'user', content: 'Show my portfolio' }],
metadata: {
user_id: 'user-123',
session_id: 'session-abc',
wallets: [{ address: '0x...', chain: 'ethereum' }],
},
});
} catch (error) {
if (error instanceof ChaosTimeoutError) {
console.error('Request timed out');
} else if (error instanceof ChaosError) {
console.error('API error:', error.message, error.status, error.code);
}
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
ChaosError
class ChaosError extends Error {
constructor(
message: string,
public readonly status?: number,
public readonly code?: string,
public readonly type?: string
);
name: 'ChaosError';
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
ChaosTimeoutError
class ChaosTimeoutError extends ChaosError {
constructor(timeoutMs: number);
name: 'ChaosTimeoutError';
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
The Chaos Client
The Chaos class exposes the following API surface:
class Chaos {
constructor(config: ChaosSDKOptions);
/** The chat namespace */
readonly chat: {
/** The responses API */
readonly responses: {
/** Create a streaming response */
create(params: ChatCreateRequestParams): Promise<ChatCreateResponse>;
/** Cancel the current in-flight request */
cancel(): void;
/** Reset the session (cancel + new session ID) */
reset(): void;
};
};
/** API key accessor */
get apiKey(): string;
/** Base URL accessor */
get baseUrl(): string;
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Session Management
The SDK automatically generates a session ID for each Responses instance. Use reset() to start a new conversation, or pass session_id in metadata to control it explicitly:
// Let the SDK manage sessions
const response1 = await chaos.chat.responses.create({ ... });
const response2 = await chaos.chat.responses.create({ ... }); // same session
chaos.chat.responses.reset(); // new session
const response3 = await chaos.chat.responses.create({ ... }); // different session
// Or control it yourself via metadata.session_id
const response = await chaos.chat.responses.create({
...,
metadata: { user_id: 'user-1', session_id: 'my-custom-session' },
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Was this helpful?