Documentation/Production Deployment
v2.0.0-beta.1Snapshot bebdcd1Markdown ↗

Production Deployment

Production Deployment

Environment Variables

env
# AI Providers
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=...

# OAuth (32-byte hex key)
OAUTH_ENCRYPTION_KEY=abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789

# Optional: Base URLs for proxies
OPENAI_BASE_URL=https://api.openai.com/v1
ANTHROPIC_BASE_URL=https://api.anthropic.com

# Note: timeouts are configured per-tool/provider in code, not via env vars —
# there is no REQUEST_TIMEOUT env var read by the library.

Error Handling

typescript
import {
  Agent,
  ProviderAuthError,
  ProviderRateLimitError,
  ProviderContextLengthError,
  ToolExecutionError,
} from 'oneringai';

const agent = Agent.create({ connector: 'openai', model: 'gpt-4.1' });

try {
  const response = await agent.run('Hello');
} catch (error) {
  if (error instanceof ProviderAuthError) {
    console.error('Authentication failed:', error.message);
    // Check API key
  } else if (error instanceof ProviderRateLimitError) {
    console.error('Rate limit exceeded:', error.message);
    // Retry with backoff
  } else if (error instanceof ProviderContextLengthError) {
    console.error('Context too long:', error.message);
    // Use context management
  } else if (error instanceof ToolExecutionError) {
    console.error('Tool failed:', error.message);
    // Handle tool error
  } else {
    console.error('Unknown error:', error);
  }
}

Best Practices

1. Use Named Connectors

typescript
// Good: Named connectors
Connector.create({ name: 'openai-main', vendor: Vendor.OpenAI,
  auth: { type: 'api_key', apiKey: process.env.OPENAI_API_KEY! } });
Connector.create({ name: 'openai-backup', vendor: Vendor.OpenAI,
  auth: { type: 'api_key', apiKey: process.env.OPENAI_BACKUP_API_KEY! } });

const agent = Agent.create({ connector: 'openai-main', model: 'gpt-4.1' });

// Bad: Passing keys directly
const agentVariant2 = Agent.create({
  connector: { vendor: Vendor.OpenAI, auth: { apiKey: '...' } },
  model: 'gpt-4.1'
});

2. Handle Rate Limits

Configure the named connector's retry contract for individual eligible requests. Do not wrap an entire tool-using agent.run() in automatic retry: an earlier tool may already have performed a write. Use bounded loop policies and original receipts to reconcile uncertain actions. retryWithBackoff is appropriate only when the host knows the entire operation is safe to repeat. Read outcomes and cancellation for operation budgets, retryPolicy: 'safe', and cancellation boundaries.

3. Monitor Context Usage

typescript
// Monitor context budget
const budget = agent.context.lastPreparation?.budget; // Inspect after run/stream; do not prepare twice.
if (budget && budget.utilizationPercent > 80) {
  console.warn(`Context at ${budget.utilizationPercent}%`);
}

4. Use Circuit Breakers

typescript
import { CircuitBreaker } from 'oneringai';

const breaker = new CircuitBreaker('my-breaker', {
  failureThreshold: 5,
  resetTimeoutMs: 30000,
});

const safeTool: ToolFunction = {
  // ...
  execute: async (args) => {
    return await breaker.execute(() => externalAPI.call(args));
  },
};

5. Secure OAuth Tokens

typescript
// Always use encryption for OAuth tokens
const oauth = new OAuthManager({
  // ...
  storage: new FileStorage({
    directory: './tokens',
    encryptionKey: process.env.OAUTH_ENCRYPTION_KEY!, // Required!
  }),
});

6. Clean Up Resources (IDisposable Pattern)

The library uses the IDisposable pattern for proper resource cleanup. All major classes implement this pattern with:

  • destroy(): void - Releases all resources (safe to call multiple times)
  • isDestroyed: boolean - Check if already destroyed
typescript
// Agent - cascades to AgentContextNextGen → ToolManager → CircuitBreakers
const agent = Agent.create({ connector: 'openai-main', model: 'gpt-4.1' });
agent.onCleanup(() => {
  console.log('Cleaning up...');
});
agent.destroy();  // Cleans up all child resources

// Standalone ToolManager
const toolManager = new ToolManager();
toolManager.destroy();  // Cleans up circuit breakers and listeners

// Check before use
if (!toolManager.isDestroyed) {
  await toolManager.execute('my_tool', args);
}

Classes implementing IDisposable:

  • Agent
  • AgentContextNextGen
  • ToolManager
  • WorkingMemoryPluginNextGen

Performance Tips

  1. Use appropriate models:

    • gpt-6-luna, gemini-3.5-flash-lite, or claude-sonnet-5-5 for low-latency/high-throughput work
    • gpt-6.1-sol, gemini-3.8-flash, or grok-4.7 for balanced production agents
    • gpt-6-astra, gpt-5.5-pro, or claude-opus-5-5 for the most demanding reasoning and long-horizon work
  2. Leverage caching:

    • Use provider-aware prompt caching where getAdvancedCapabilities() reports support
    • Keep stable prompt prefixes first and record actual cache-hit telemetry
  3. Use streaming:

    • Better user experience
    • Lower perceived latency
  4. Manage context:

    • The default algorithmic strategy (75% reclaim target; 90% critical trigger) handles most use cases
    • Enable workingMemory for automatic tool result offloading
    • Register custom strategies via StrategyRegistry for specialized needs
  5. Batch requests:

    • Use getBatchProvider() for supported offline workloads
    • Persist handles and reconcile ambiguous submissions instead of retrying blindly

Documentation snapshot · 2026-11-02Suggest a correction ↗

Search documentation

Type to search the v2 documentation.