Document ID: PRT-2024-001 Revision: 2.4 Date: 2024

AI API Proxy Porting Guide

Technical implementation manual for porting existing AI integrations to a unified proxy architecture. Includes code samples, API mapping tables, and step-by-step migration procedures.

Difficulty
Medium
Time Estimate
2-5 Days
Prerequisites
API Access
Downtime
Zero
§ 1.0

Architecture Overview

Your Application

  • Existing AI integrations
  • Direct API calls
  • Multiple SDKs
  • Provider-specific code
→ → →

Unified Proxy

  • Single endpoint
  • Request translation
  • Response normalization
  • Caching layer
→ → →

Multiple Providers

  • OpenAI (GPT-4/3.5)
  • Anthropic (Claude)
  • Google (Gemini)
  • Other providers
§ 2.0

Code Migration Examples

before.js LEGACY
// Direct OpenAI Integration import OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.OPENAI_KEY }); const completion = await client.chat.completions.create({ model: 'gpt-4', messages: [ { role: 'system', content: 'You are helpful' }, { role: 'user', content: userInput } ], temperature: 0.7, max_tokens: 2000 }); // Provider-specific error handling try { const response = await completion; return response.choices[0].message.content; } catch (error) { if (error.code === 'rate_limit') { // Custom retry logic } }
after.js GATEWAY
// Unified Gateway Integration import Gateway from '@gateway/sdk'; const gateway = new Gateway({ apiKey: process.env.GATEWAY_KEY }); const completion = await gateway.chat({ model: 'gpt-4', // or any other model messages: [ { role: 'system', content: 'You are helpful' }, { role: 'user', content: userInput } ], temperature: 0.7, max_tokens: 2000, // Built-in features fallback: true, cache: true, retries: 3 }); // Unified error handling return completion.choices[0].message.content;
§ 3.0

Porting Procedure

STEP 01

Environment Setup

Install gateway SDK, configure credentials, and establish staging environment for parallel testing.

Install @gateway/sdk package
Set GATEWAY_API_KEY environment variable
Create staging configuration
STEP 02

API Mapping

Map existing provider-specific parameters to gateway standard format. Document any custom logic.

Identify all model references
Map parameter translations
Document custom error handling
STEP 03

Code Refactoring

Replace direct API calls with gateway methods. Update imports, remove provider-specific SDKs.

Replace OpenAI/Anthropic imports
Update API call patterns
Simplify error handling
STEP 04

Testing & Validation

Run parallel tests comparing direct API vs gateway responses. Validate accuracy and latency.

Compare response structures
Test edge cases
Measure performance metrics
§ 4.0

Provider Compatibility Matrix

Feature OpenAI Anthropic Google Mistral
Chat Completions ✓ Supported ✓ Supported ✓ Supported ✓ Supported
Streaming ✓ Supported ✓ Supported △ Partial ✓ Supported
Function Calling ✓ Supported △ Partial △ Partial ✓ Supported
Vision ✓ Supported ✓ Supported ✓ Supported △ Partial
Embeddings ✓ Supported ✗ N/A ✓ Supported ✓ Supported
§ 5.0

Important Considerations

Rate Limiting

Gateway implements unified rate limiting across all providers. Default: 1000 requests/minute. Configure limits per endpoint if needed.

Response Format

All responses normalized to OpenAI-compatible format. Provider-specific fields available in metadata object for advanced use cases.

Error Codes

Provider-specific error codes translated to standard HTTP status codes. Original error details preserved in response body.

Implementation Support

Technical documentation complete. Access SDK and start porting process with dedicated support.

Begin Porting