SDK Overview
Updated
The @chaoslabs/ai-sdk is a TypeScript SDK for building AI-powered DeFi applications. It provides a streaming client, typed block responses, risk analysis utilities, and helpers for working with on-chain transaction primitives.
Installation
npm install @chaoslabs/ai-sdkCopy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Or with other package managers:
# yarn
yarn add @chaoslabs/ai-sdk
# pnpm
pnpm add @chaoslabs/ai-sdk
# bun
bun add @chaoslabs/ai-sdkCopy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Quick Start
Create a client, pick a model, and send a query:
import { Chaos, WALLET_MODEL, extractText, extractBlocks } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({
apiKey: process.env.CHAOS_API_KEY!,
});
const response = await chaos.chat.responses.create({
model: WALLET_MODEL,
input: [
{ type: 'message', role: 'user', content: 'What is in my portfolio?' }
],
metadata: {
user_id: 'user-123',
session_id: 'session-abc',
wallets: [{ address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', chain: 'ethereum' }],
},
});
console.log('Status:', response.status);
console.log('Text:', extractText(response));
console.log('Blocks:', extractBlocks(response).length);Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Models
The SDK exposes a model constant that determines the type of AI interaction:
| Constant | Value | Purpose |
|---|---|---|
WALLET_MODEL | 'WALLET_MODEL' | Wallet mode for DeFi operations. Requires wallets in metadata. Returns transaction primitives, risk analysis, tables, and charts. |
import { Chaos, WALLET_MODEL } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({ apiKey: process.env.CHAOS_API_KEY! });
const walletResponse = await chaos.chat.responses.create({
model: WALLET_MODEL,
input: [{ type: 'message', role: 'user', content: 'Deposit 1 ETH into Aave on Ethereum' }],
metadata: {
user_id: 'user-123',
session_id: 'session-abc',
wallets: [{ address: '0x...', chain: 'ethereum' }],
},
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Environment Variables
| Variable | Required | Description |
|---|---|---|
CHAOS_API_KEY | Yes | Your Chaos Labs API key (starts with ck-) |
Streaming Events
The SDK streams responses by default. Use the onStreamEvent callback to receive events as they arrive:
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' }],
},
onStreamEvent: (message) => {
// ChaosSDKMessage — status, text, block, follow_up_suggestions, or input
console.log(`[${message.type}]`, message.data);
},
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Response Structure
The SDK uses a layered type hierarchy. Every API call returns a containing typed messages, blocks, and primitives:
Use the SDK's type guards to narrow each layer: → → access typed .
Core Concepts
- Configuration — Client options, timeouts, and base URL
- Types Reference — Request/response types and interfaces
- Response Handling — Extract text, blocks, and check risks
- Block Types — 10 block types with TypeScript interfaces
- Helper Functions — Block extraction, search, table/chart/risk utilities
Was this helpful?