API Gateway Proxy for VSCode

Build powerful AI-powered VSCode extensions with unified model access. Implement intelligent code completion, chat interfaces, and smart refactoring tools using a single gateway proxy that handles multiple AI providers automatically.

Multi-provider AI model access
Automatic failover and retry logic
Intelligent response caching
Built-in rate limit handling
Zero-config provider switching
extension.ts gateway.ts
1import { GatewayClient } from 'ai-gateway';
2
3const gateway = new GatewayClient({
4 providers: ['openai', 'anthropic'],
5 cache: { enabled: true }
6});
7
8export function activate(context) {
9 // Register AI completion provider

Getting Started

Integrating an API gateway proxy into your VSCode extension dramatically simplifies AI integration while providing enterprise-grade reliability features. Instead of managing multiple SDKs, authentication flows, and error handling for each AI provider, your extension communicates with a single, intelligent gateway endpoint.

The gateway handles provider selection, automatic failover, response caching, and rate limit management transparently. This architectural separation allows your extension code to focus on user experience features rather than infrastructure concerns, resulting in cleaner, more maintainable codebases.

Installation Steps

  1. Install Gateway SDK Install the official gateway client package via npm. This lightweight SDK handles all communication with your gateway server and provides TypeScript definitions for enhanced IDE support.
  2. Configure Extension Settings Add gateway endpoint URL and authentication credentials to your extension's configuration schema. VSCode's settings system provides built-in UI for users to configure these values securely.
  3. Initialize Gateway Client Create a gateway client instance in your extension's activate function. The client establishes persistent connections and manages connection pooling automatically for optimal performance.
  4. Register Providers Use VSCode's language provider APIs to register completion, hover, or code action providers that leverage the gateway for AI-powered functionality.
  5. Handle User Authentication Implement VSCode's SecretStorage API to securely store user API keys. Never store credentials in plain text settings or workspace configurations.
package.json - Extension Manifest
{
  "name": "ai-code-assistant",
  "version": "1.0.0",
  "engines": { "vscode": "^1.85.0" },
  "activationEvents": ["onStartupFinished"],
  "main": "./out/extension.js",
  "contributes": {
    "configuration": {
      "title": "AI Assistant",
      "properties": {
        "aiAssistant.gatewayEndpoint": {
          "type": "string",
          "default": "https://gateway.example.com",
          "description": "Gateway proxy endpoint URL"
        },
        "aiAssistant.defaultProvider": {
          "type": "string",
          "enum": ["openai", "anthropic", "auto"],
          "default": "auto"
        }
      }
    }
  }
}

Extension Architecture

A well-structured VSCode extension separates concerns cleanly: UI logic, gateway communication, and business logic each live in distinct modules. This separation simplifies testing, enables parallel development, and makes future provider additions straightforward.

VSCode UI

Editor & Commands

Extension Host

Provider Logic

Gateway Client

SDK & Cache

Gateway Server

Routing & Auth

AI Providers

OpenAI, Claude...

Core Extension Implementation

The extension's activate function initializes the gateway client and registers language providers. Using VSCode's built-in dependency injection, providers receive the gateway client through constructor parameters, enabling easy mocking for unit tests.

extension.ts - Main Entry Point
import * as vscode from 'vscode';
import { GatewayClient } from 'ai-gateway-sdk';
import { CompletionProvider } from './providers/completion';

export async function activate(context: vscode.ExtensionContext) {
    // Initialize gateway with secure credentials
    const gateway = new GatewayClient({
        endpoint: vscode.workspace.getConfiguration('aiAssistant')
            .get('gatewayEndpoint'),
        apiKey: await context.secrets.get('apiKey'),
        cache: { 
            enabled: true, 
            ttl: 3600,
            maxSize: 1000
        }
    });

    // Register inline completion provider for all languages
    const completionProvider = vscode.languages
        .registerInlineCompletionItemProvider(
            { pattern: '**/*' },
            new CompletionProvider(gateway)
        );
    
    context.subscriptions.push(completionProvider);
}

Key Features Enabled

The gateway proxy unlocks capabilities that would require significant custom development when integrating AI providers directly. These features enhance user experience while reducing your extension's complexity and maintenance burden.

With Gateway Proxy

  • Single endpoint for all providers
  • Automatic failover on errors
  • Built-in response caching
  • Transparent rate limit handling
  • Usage analytics dashboard
  • Cost optimization routing
  • Unified error handling

Without Gateway

  • Multiple SDK integrations
  • Manual error retry logic
  • Custom cache implementation
  • Complex rate limit code
  • Build analytics from scratch
  • Fixed provider per request
  • Provider-specific errors

Intelligent Code Completion

The most common use case for AI-powered extensions is inline code completion. The gateway enhances this feature by automatically selecting the optimal model based on context—simple completions use fast, cost-effective models while complex logic benefits from larger reasoning models.

completion-provider.ts
export class CompletionProvider 
    implements vscode.InlineCompletionItemProvider {
    
    constructor(private gateway: GatewayClient) {}

    async provideInlineCompletionItems(
        document: vscode.TextDocument,
        position: vscode.Position,
        context: vscode.InlineCompletionContext,
        token: vscode.CancellationToken
    ): Promise<vscode.InlineCompletionItem[]> {
        
        const prefix = document.getText(
            new vscode.Range(
                Math.max(0, position.line - 50), 
                0, 
                position.line, 
                position.character
            )
        );

        try {
            const response = await this.gateway.complete({
                prompt: prefix,
                language: document.languageId,
                maxTokens: 100
            });

            return [
                new vscode.InlineCompletionItem(
                    response.completion,
                    new vscode.Range(position, position)
                )
            ];
        } catch (error) {
            // Gateway handles fallback automatically
            return [];
        }
    }
}

Provider Configuration Comparison

Different AI providers have varying strengths, costs, and latency characteristics. Your gateway configuration should balance these factors based on your extension's use cases and target users. Enterprise extensions might prioritize reliability and features, while consumer extensions optimize for cost efficiency.

Provider Strength Latency Best For
OpenAI GPT-4 Complex reasoning 2-5 seconds Code generation, analysis
OpenAI GPT-3.5 Speed & cost 0.5-1.5 seconds Simple completions
Claude Long context 1-3 seconds Large files, documentation
Code-specific models Code accuracy 0.5-2 seconds Inline suggestions

Partner Resources