AgentSea

Package

@lov3kaizen/agentsea-gateway

An LLM gateway with an OpenAI-compatible API over OpenAI, Anthropic and Google providers, with routing, caching and metrics.

v1.2.1On npm

01Install

Install from npm into your project.

BASH
pnpm add @lov3kaizen/agentsea-gateway

02Example

TS
import { Gateway } from '@lov3kaizen/agentsea-gateway';

const gateway = new Gateway({
  providers: [{ name: 'openai', apiKey: process.env.OPENAI_API_KEY, models: ['gpt-5.5'] }],
});
const response = await gateway.chat.completions.create({
  model: 'gpt-5.5',
  messages: [{ role: 'user', content: 'Hello!' }],
});
if ('choices' in response) {
  console.log(response.choices[0].message.content);
  console.log(response._gateway); // provider, cost, latency
}

03Key API

new Gateway(config)
Creates a gateway from a list of providers and optional routing and cache settings.
gateway.chat.completions.create(request)
Sends an OpenAI-style chat request to the chosen provider.
routing.strategy
One of round-robin, failover, cost-optimized or latency-optimized.
model: 'best' | 'cheapest' | 'fastest'
Virtual models that route each request automatically.
gateway.getMetrics()
Returns request count, cost, latency and cache hit rate.

04Caveats

  • How CostOptimizedStrategy measures quality is not described.
  • The circuit breaker for failover is mentioned but not configured in the docs.
  • The cache is described as exact-match with maxEntries and ttl. No LRU policy is documented.
  • Events request:complete, request:error, provider:unhealthy and provider:healthy are emitted by the gateway. See Events and metrics below.

05Routing

Set routing in the gateway config. strategy picks the default behaviour. Use weights with round-robin, and fallbackChain with failover.

TS
const gateway = new Gateway({
  providers: [/* ... */],
  routing: {
    strategy: 'failover',
    fallbackChain: ['openai', 'anthropic', 'google'],
  },
});
  • round-robin with weights, for example { openai: 2, anthropic: 1 }, sends twice as many requests to OpenAI.
  • failover tries the providers in fallbackChain order until one succeeds.
  • cost-optimized and latency-optimized choose by cost and by observed latency.

06Caching

Turn on the cache to reuse responses for repeated requests. ttl is in seconds. maxEntries caps the number of stored responses.

TS
const gateway = new Gateway({
  providers: [/* ... */],
  cache: {
    enabled: true,
    ttl: 3600, // seconds
    maxEntries: 1000,
    type: 'exact', // or 'semantic'
  },
});

07Request metadata

Add a _gateway object to a request to steer routing or the cache for that call only.

TS
const response = await gateway.chat.completions.create({
  model: 'gpt-5.5',
  messages: [{ role: 'user', content: 'Hello!' }],
  _gateway: {
    preferredProvider: 'anthropic',
    excludeProviders: ['google'],
    maxCost: 0.01,
    maxLatency: 5000,
    cachePolicy: 'no-cache', // 'default' | 'no-cache' | 'force-cache'
    tags: { user: 'user-123' },
  },
});

08HTTP server

Run the gateway as an OpenAI-compatible HTTP server, then point any OpenAI SDK or curl at it.

TS
import { Gateway, createHTTPServer, startServer } from '@lov3kaizen/agentsea-gateway';

const gateway = new Gateway({
  providers: [{ name: 'openai', apiKey: process.env.OPENAI_API_KEY, models: ['gpt-5.5'] }],
});
const app = createHTTPServer({ gateway });
startServer(app, { port: 3000 });
BASH
curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{ "model": "cheapest", "messages": [{"role": "user", "content": "Hello!"}] }'

09Events and metrics

TS
gateway.on('request:complete', (event) => {
  console.log(event.provider, event.latencyMs, event.cost);
});
gateway.on('request:error', (event) => {
  console.error(event.error.message);
});
gateway.on('provider:unhealthy', (provider) => {
  console.warn(provider, 'is unhealthy');
});

const metrics = gateway.getMetrics();
console.log(metrics.requests.total, metrics.requests.failed);
request:complete
Fires after each request with provider, model, latencyMs, cost, cached and tokens.
request:error
Fires when a request fails, with the requestId, provider and error.
provider:unhealthy / provider:healthy
Fires when a provider is marked unhealthy or recovers.