The official TypeScript SDK for seamless integration with Agentbase agents.

Installation

npm install agentbase-sdk
# or
yarn add agentbase-sdk

Quick Start

import { Agentbase } from 'agentbase-sdk';

const agentbase = new Agentbase({
  apiKey: 'your-api-key'
});

// Run an agent
const response = await agentbase.runAgent({
  message: "Hello, analyze this data for me"
});

// Handle streaming response
for await (const event of response) {
  console.log(event);
}

Configuration

const agentbase = new Agentbase({
  apiKey: 'your-api-key',
  baseURL: 'https://api.agentbase.sh', // optional
  timeout: 30000 // optional, in milliseconds
});

API Methods

runAgent()

const stream = await agentbase.runAgent({
  message: "Your task here",
  mode: "fast", // optional: "fast", "flash", "max"
  system: "You are a helpful assistant", // optional
  rules: ["Be concise", "Show examples"], // optional
  session: "session-id", // optional for conversation continuity
  streaming: true, // optional, defaults to true
  mcpServers: [ // optional
    {
      serverName: "my-api",
      serverUrl: "https://api.example.com/mcp"
    }
  ]
});

getMessages()

const messages = await agentbase.getMessages({
  session: "session-id"
});

clearMessages()

await agentbase.clearMessages({
  session: "session-id"
});

Error Handling

try {
  const response = await agentbase.runAgent({
    message: "Your task"
  });
} catch (error) {
  if (error instanceof AgentbaseError) {
    console.error('API Error:', error.message);
    console.error('Status:', error.status);
  } else {
    console.error('Unexpected error:', error);
  }
}

TypeScript Types

interface RunAgentParams {
  message: string;
  mode?: "fast" | "flash" | "max";
  system?: string;
  rules?: string[];
  session?: string;
  streaming?: boolean;
  mcpServers?: MCPServer[];
}

interface MCPServer {
  serverName: string;
  serverUrl: string;
}

interface AgentEvent {
  type: string;
  content?: string;
  session?: string;
  cost?: string;
  balance?: number;
}

Examples

Basic Chat

const chat = await agentbase.runAgent({
  message: "What's the weather like today?"
});

for await (const event of chat) {
  if (event.type === 'agent_message') {
    console.log('Agent:', event.content);
  }
}

With Session Continuity

let sessionId: string | undefined;

// First message
const response1 = await agentbase.runAgent({
  message: "Hello, I'm working on a React project"
});

for await (const event of response1) {
  if (event.session) sessionId = event.session;
}

// Continue conversation
const response2 = await agentbase.runAgent({
  message: "Can you help me with state management?",
  session: sessionId
});

Cost Tracking

let totalCost = 0;

const response = await agentbase.runAgent({
  message: "Analyze this dataset"
});

for await (const event of response) {
  if (event.type === 'agent_cost') {
    totalCost += parseFloat(event.cost || '0');
    console.log(`Current cost: $${event.cost}`);
    console.log(`Total spent: $${totalCost}`);
  }
}

Resources

What’s Next?