Production Deployment
Production Deployment
Environment Variables
# 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
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
// 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
// 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
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
// 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
// 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:
AgentAgentContextNextGenToolManagerWorkingMemoryPluginNextGen
Performance Tips
-
Use appropriate models:
gpt-6-luna,gemini-3.5-flash-lite, orclaude-sonnet-5-5for low-latency/high-throughput workgpt-6.1-sol,gemini-3.8-flash, orgrok-4.7for balanced production agentsgpt-6-astra,gpt-5.5-pro, orclaude-opus-5-5for the most demanding reasoning and long-horizon work
-
Leverage caching:
- Use provider-aware prompt caching where
getAdvancedCapabilities()reports support - Keep stable prompt prefixes first and record actual cache-hit telemetry
- Use provider-aware prompt caching where
-
Use streaming:
- Better user experience
- Lower perceived latency
-
Manage context:
- The default
algorithmicstrategy (75% reclaim target; 90% critical trigger) handles most use cases - Enable
workingMemoryfor automatic tool result offloading - Register custom strategies via
StrategyRegistryfor specialized needs
- The default
-
Batch requests:
- Use
getBatchProvider()for supported offline workloads - Persist handles and reconcile ambiguous submissions instead of retrying blindly
- Use