Skip to main content

AI Gateway for Developers

If you’re building applications that use AI, the AI Gateway provides essential features for production deployments: user management, abuse prevention, cost tracking, and optimization tools.

Why Developers Need AI Gateway

When you release an AI-powered app to users, you face several challenges:

Malicious Users

Users may try to abuse your AI features, running up costs or extracting your prompts

Cost Overruns

Without limits, a few heavy users can consume your entire AI budget

No Visibility

You can’t see how users are actually using your AI features

Optimization Blind Spots

You don’t know which prompts or models perform best

Key Features for Developers

1. User-Level Tracking

Track AI usage per user in your application:
const response = await client.chat.completions.create({
  model: 'gpt-5.2',
  messages: [{ role: 'user', content: userMessage }],
}, {
  headers: {
    'X-User-ID': userId,
    'X-Session-ID': sessionId,
  }
});
Per-user tracking dashboard
This enables:
  • Usage limits per user - prevent abuse
  • Cost attribution - know who’s using what
  • Behavior analysis - understand usage patterns

2. Abuse Prevention

Block malicious users
Protect your application from abuse:
  • Rate limiting - limit requests per user/minute
  • User blocking - instantly block abusive users
  • Pattern detection - identify suspicious usage patterns
  • Cost caps - set maximum spend per user
// Block a user via API
await leanmcp.gateway.blockUser({
  userId: 'abusive-user-123',
  reason: 'Excessive usage detected',
});

3. Competitor Intelligence

Understand how similar applications use AI:
Competitor analysis
  • Prompt patterns - see what prompts work well
  • Model choices - understand which models others use
  • Token efficiency - compare your usage to benchmarks
  • Best practices - learn from successful implementations

4. A/B Testing

Test different prompts and models to optimize performance:
// A/B test different prompts
const variant = await leanmcp.gateway.getVariant({
  experimentId: 'prompt-optimization-v1',
  userId: userId,
});

const systemPrompt = variant === 'A' 
  ? 'You are a helpful assistant.' 
  : 'You are an expert software engineer.';

const response = await client.chat.completions.create({
  model: 'gpt-5.2',
  messages: [
    { role: 'system', content: systemPrompt },
    { role: 'user', content: userMessage }
  ],
}, {
  headers: {
    'X-Experiment-ID': 'prompt-optimization-v1',
    'X-Variant': variant,
  }
});
A/B testing results
Track and compare:
  • Response quality - user satisfaction metrics
  • Token usage - cost per variant
  • Latency - response time differences
  • Conversion rates - business impact

Integration Guide

Basic Setup

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://aigateway.leanmcp.com/v1/openai',
  apiKey: process.env.LEANMCP_API_KEY,
});

// All requests now go through the gateway
const response = await client.chat.completions.create({
  model: 'gpt-5.2',
  messages: [{ role: 'user', content: 'Hello!' }],
});

Adding User Context

async function generateResponse(userId: string, sessionId: string, message: string) {
  return await client.chat.completions.create({
    model: 'gpt-5.2',
    messages: [{ role: 'user', content: message }],
  }, {
    headers: {
      'X-User-ID': userId,
      'X-Session-ID': sessionId,
      'X-Request-Source': 'web-app',
    }
  });
}

Implementing Rate Limits

Set up rate limits in your dashboard or via API:
// Configure rate limits
await leanmcp.gateway.setRateLimit({
  userId: userId,
  limits: {
    requestsPerMinute: 10,
    tokensPerDay: 100000,
    maxCostPerMonth: 50.00,
  }
});

Dashboard Features

Usage Analytics

Developer analytics dashboard
  • Request volume over time
  • Token usage by model and user
  • Cost breakdown by feature and user segment
  • Error rates and failure analysis

User Management

User management
  • View all users and their usage
  • Set individual limits and permissions
  • Block or restrict users
  • Export usage data

Alerts & Monitoring

Set up alerts for:
  • Unusual usage spikes
  • Budget thresholds
  • Error rate increases
  • Specific user behaviors

Production Best Practices

Include X-User-ID and X-Session-ID to enable per-user tracking and limits.
Configure maximum spend limits before launch to prevent surprises.
Watch your dashboard closely during launch to catch abuse early.
Continuously optimize your prompts and model choices with experiments.
Regularly check what’s being blocked to tune your security rules.

API Reference

Full API documentation for gateway management:
// Gateway Management API
leanmcp.gateway.blockUser({ userId, reason })
leanmcp.gateway.unblockUser({ userId })
leanmcp.gateway.setRateLimit({ userId, limits })
leanmcp.gateway.getUsage({ userId, dateRange })
leanmcp.gateway.getVariant({ experimentId, userId })
leanmcp.gateway.recordOutcome({ experimentId, userId, outcome })

Next Steps