Skip to main content

Installation

npm install agi

Quick Reference

Always clean up sessions. The packages handle cleanup automatically.
import { AGIClient } from 'agi';

const client = new AGIClient({ apiKey: 'your_api_key' });

// Usage - manual cleanup
const session = await client.createSession('agi-0');
try {
    const result = await session.runTask('Find iPhone 15 Pro prices');
} finally {
    await session.delete();
}
Always delete sessions. Failing to clean up leads to resource exhaustion and unexpected billing.

Choose the Right Agent

AgentUse ForSpeed
agi-0Complex tasks, analysis, multi-step researchStandard
agi-0-fastSimple tasks, price checks, data extractionFast
// Simple task → fast agent
const session1 = await client.createSession('agi-0-fast');
try {
    const result = await session1.runTask('Get iPhone 15 price on Amazon');
} finally {
    await session1.delete();
}

// Complex task → standard agent
const session2 = await client.createSession('agi-0');
try {
    const result = await session2.runTask(`
        Research iPhone 15 Pro:
        1. Compare specs with competitors
        2. Analyze reviews
        3. Find best prices
    `);
} finally {
    await session2.delete();
}
Use one session for multiple related tasks to maintain context.
const session = await client.createSession('agi-0');
try {
    // Task 1: Get specs
    const specs = await session.runTask('Find iPhone 15 Pro specifications');
    
    // Task 2: Find prices (same session maintains context)
    const prices = await session.runTask('Find iPhone 15 Pro prices at major retailers');
} finally {
    await session.delete();
}

Preserve Authentication

Save snapshots to maintain logged-in state across sessions.
// After logging in, save snapshot
const session = await client.createSession('agi-0');
await session.runTask('Login to example.com');
await session.delete({ 
    saveSnapshotMode: 'filesystem', 
    saveAsDefault: true 
});

// Future sessions auto-restore authentication
const newSession = await client.createSession('agi-0', {
    restoreDefaultEnvironmentFromUserId: user_id
});
Use snapshots to preserve cookies, authentication tokens, and browser state.

Best Practices

Always Cleanup

Use try-finally or context managers to ensure session deletion

Choose Right Model

Use agi-0-fast for simple tasks, agi-0 for complex ones

Reuse When Appropriate

Use single session for related sequential tasks

Preserve Authentication

Use snapshots to maintain logged-in sessions