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.

[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Installation

terminal
npm install @chaoslabs/ai-sdk

Or with other package managers:

terminal
# yarn
yarn add @chaoslabs/ai-sdk

# pnpm
pnpm add @chaoslabs/ai-sdk

# bun
bun add @chaoslabs/ai-sdk

Quick Start

Create a client, pick a model, and send a query:

quickstart.ts
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);

Models

The SDK exposes a model constant that determines the type of AI interaction:

ConstantValuePurpose
WALLET_MODEL'WALLET_MODEL'Wallet mode for DeFi operations. Requires wallets in metadata. Returns transaction primitives, risk analysis, tables, and charts.
models.ts
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' }],
  },
});

Environment Variables

VariableRequiredDescription
CHAOS_API_KEYYesYour Chaos Labs API key (starts with ck-)
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Streaming Events

The SDK streams responses by default. Use the onStreamEvent callback to receive events as they arrive:

streaming.ts
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);
  },
});

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

Was this helpful?