This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is the Portkey AI Gateway - a fast, reliable AI gateway that routes requests to 250+ LLMs with sub-1ms latency. It's built with Hono framework for TypeScript/JavaScript and can be deployed to multiple environments including Cloudflare Workers, Node.js servers, and Docker containers.
npm run dev- Start development server using Wrangler (Cloudflare Workers)npm run dev:node- Start development server using Node.jsnpm run build- Build the project for productionnpm run build-plugins- Build the plugin system
npm run test:gateway- Run tests for the main gateway code (src/)npm run test:plugins- Run tests for pluginsjest src/- Run specific gateway testsjest plugins/- Run specific plugin tests
npm run format- Format code with Prettiernpm run format:check- Check code formattingnpm run pretty- Alternative format command
npm run deploy- Deploy to Cloudflare Workersnpm run start:node- Start production Node.js server
Main Application (src/index.ts)
- Hono-based HTTP server with middleware pipeline
- Handles multiple AI provider integrations
- Routes:
/v1/chat/completions,/v1/completions,/v1/embeddings, etc.
Provider System (src/providers/)
- Modular provider implementations (OpenAI, Anthropic, Azure, etc.)
- Each provider has standardized interface:
api.ts,chatComplete.ts,embed.ts - Provider configs define supported features and transformations
Middleware Pipeline
requestValidator- Validates incoming requestshooks- Pre/post request hooksmemoryCache- Response cachinglogger- Request/response loggingportkey- Core Portkey-specific middleware for routing, guardrails, etc.
Plugin System (plugins/)
- Guardrail plugins for content filtering, PII detection, etc.
- Each plugin has
manifest.jsondefining capabilities - Plugins are built separately with
npm run build-plugins
Configs - JSON configurations that define:
- Provider routing and fallbacks
- Load balancing strategies
- Guardrails and content filtering
- Caching and retry policies
Handlers - Route-specific request processors in src/handlers/
- Each AI API endpoint has dedicated handler
- Stream handling for real-time responses
- WebSocket support for realtime APIs
src/providers/- AI provider integrationssrc/handlers/- API endpoint handlerssrc/middlewares/- Request/response middlewareplugins/- Guardrail and validation pluginscookbook/- Example integrations and use casesconf.json- Runtime configuration
Tests are organized by component:
src/tests/- Core gateway functionality testssrc/handlers/__tests__/- Handler-specific testsplugins/*/**.test.ts- Plugin tests- Test timeout: 30 seconds (configured in jest.config.js)
The gateway uses conf.json for runtime configuration. Sample config available in conf_sample.json.
Key environment variables and configuration handled through Hono's adapter system for multi-environment deployment.