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
- 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.
- 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.
- 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.
- Register Providers Use VSCode's language provider APIs to register completion, hover, or code action providers that leverage the gateway for AI-powered functionality.
- Handle User Authentication Implement VSCode's SecretStorage API to securely store user API keys. Never store credentials in plain text settings or workspace configurations.
{
"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.
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.
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.
Partner Resources
OpenAI API Gateway Fallback Models
Implement intelligent failover chains for maximum reliability.
AI API Gateway for IDE Plugins
Universal guide for IDE extension development.
AI API Proxy for JetBrains
Build intelligent plugins for IntelliJ-based IDEs.
LLM API Gateway for Cursor
Extend Cursor AI with custom gateway integrations.