Skip to main content

Availability

Edge Gateway plugins provide middleware hooks in the LLM proxy request/response pipeline using the Unified Plugin SDK. Use them for custom authentication, request/response transformation, content filtering, and data collection to external systems. All Edge Gateway plugins now use pkg/plugin_sdk, which automatically detects the Gateway runtime and provides access to universal services (KV storage, logging) and Gateway-specific services (app management, budget status).

Plugin Capabilities

Edge Gateway plugins implement one or more of these capability interfaces:

1. PreAuthHandler

Interface: PreAuthHandler Method: HandlePreAuth(ctx Context, req *pb.EnrichedRequest) (*pb.PluginResponse, error) Executes before authentication. Use for:
  • Request validation and early rejection
  • Request enrichment with metadata
  • Header modification
  • Logging and auditing
Working Example: examples/plugins/gateway/request_enricher/

2. AuthHandler

Interface: AuthHandler Method: HandleAuth(ctx Context, req *pb.EnrichedRequest) (*pb.PluginResponse, error) Replaces default token authentication. Use for:
  • Custom authentication schemes (OAuth, JWT, API keys)
  • Integration with external identity providers
  • Multi-factor authentication
  • Custom authorization logic
Note: Unified SDK provides credential validation via ctx.Services.Gateway().ValidateCredential()

3. PostAuthHandler

Interface: PostAuthHandler Method: HandlePostAuth(ctx Context, req *pb.EnrichedRequest) (*pb.PluginResponse, error) Executes after authentication. Most common capability for gateway plugins. Use for:
  • Enriching requests with user-specific data
  • Per-user request transformation
  • Access control enforcement
  • Usage quota checks
Working Example: examples/plugins/gateway/request_enricher/

4. ResponseHandler

Interface: ResponseHandler Methods:
  • OnBeforeWriteHeaders(ctx Context, req *pb.ResponseWriteRequest) (*pb.ResponseWriteResponse, error)
  • OnBeforeWrite(ctx Context, req *pb.ResponseWriteRequest) (*pb.ResponseWriteResponse, error)
Modifies LLM responses before returning to client. Two-phase processing:
  • OnBeforeWriteHeaders: Modify response headers
  • OnBeforeWrite: Modify response body
Use for:
  • Response filtering and content moderation
  • Response transformation and formatting
  • Injecting additional metadata
  • Response validation
Working Example: examples/plugins/gateway/response_modifier/

5. DataCollector

Interface: DataCollector Methods:
  • HandleProxyLog(ctx Context, log *pb.ProxyLogData) error
  • HandleAnalytics(ctx Context, analytics *pb.AnalyticsData) error
  • HandleBudgetUsage(ctx Context, usage *pb.BudgetUsageData) error
Intercepts data before database storage. Use for:
  • Exporting proxy logs to external systems
  • Sending analytics to data warehouses
  • Custom budget tracking
  • Real-time monitoring and alerting
Working Examples:

6. CustomEndpointHandler

Interface: CustomEndpointHandler Methods:
  • GetEndpointRegistrations() ([]*pb.EndpointRegistration, error)
  • HandleEndpointRequest(ctx Context, req *pb.EndpointRequest) (*pb.EndpointResponse, error)
  • HandleEndpointRequestStream(ctx Context, req *pb.EndpointRequest, stream grpc.ServerStreamingServer[pb.EndpointResponseChunk]) error
Registers and serves custom HTTP endpoints under /plugins/{slug}/. Plugins have full control over the response. Use for:
  • Custom APIs (OAuth endpoints, webhooks, health checks)
  • MCP Streamable HTTP proxy servers
  • Protocol-specific proxies
  • Any endpoint that doesn’t fit the LLM/Tool/Datasource model
Supports both unary responses and streaming (SSE) via the stream_response flag on endpoint registrations. Full Guide: Custom Endpoints Guide

Quick Start

1. Project Setup

2. Implement Plugin Structure

Use the unified SDK with BasePlugin convenience struct:
Expandable

3. Implement Capability Interfaces

Implement one or more capability interfaces based on your needs:

PostAuthHandler (Most Common)

Expandable

PreAuthHandler

Expandable

ResponseHandler

Expandable

DataCollector

Expandable

4. Build Plugin

5. Deploy Plugin

Create plugin in AI Studio dashboard or via API:

6. Attach to LLM

Associate the plugin with an LLM to activate it:

Configuration Schema

Provide JSON Schema for plugin configuration using the ConfigSchemaProvider interface:
Example config.schema.json:
Expandable
Configuration values are passed to Initialize() and can be updated via the API.

Complete Examples

Example 1: Custom Authentication Plugin

This example uses the Unified SDK for consistency with other gateway plugins like llm-firewall and llm-cache.
Expandable
Working Example: See plugins/llm-firewall/ for a production-ready content filtering plugin using this pattern.

Example 2: Elasticsearch Data Collector

Expandable

Plugin Context

The PluginContext provides contextual information about the request:
Use this context for logging, tracing, and per-request customization.

Testing Your Plugin

Unit Testing

Expandable

Integration Testing

Use file:// deployment to test with real LLM requests:
Expandable

Best Practices

Performance

  • Keep plugin logic lightweight and fast
  • Use timeouts for external API calls
  • Implement connection pooling for external services
  • Cache frequently accessed data
  • Return early for requests that don’t need processing

Error Handling

  • Log errors with context (request ID, LLM ID, etc.)
  • Return descriptive error messages
  • Don’t panic - return errors properly
  • Implement graceful degradation

Security

  • Validate all configuration inputs
  • Sanitize user-provided data
  • Use secure connections for external services
  • Don’t log sensitive data (tokens, PII)
  • Implement rate limiting for external calls

Configuration

  • Provide sensible defaults
  • Use JSON Schema for validation
  • Document all configuration options
  • Support configuration updates without restart

Troubleshooting

  • Check plugin command path is absolute with file://
  • Verify plugin binary has execute permissions
  • Check logs for initialization errors
  • Ensure plugin implements all required interfaces
  • Check plugin logs for panics
  • Verify external service connectivity
  • Test with minimal configuration
  • Use defensive error handling
  • Profile plugin with Go profiler
  • Check for blocking operations
  • Monitor external service latency
  • Review resource usage (CPU, memory)

Gateway Services

Gateway plugins have access to Gateway-specific services via ctx.Services.Gateway():
See Service API Reference for complete Gateway Services documentation.

Sending Data to Control Plane

Gateway plugins can send data back to AI Studio (the control plane) using the SendToControl API. This is useful for:
  • Aggregating statistics from edge instances
  • Synchronizing state across the hub-and-spoke architecture
  • Sending alerts or notifications to central plugins
Expandable
The control plane plugin receives this data via the EdgePayloadReceiver interface. See Edge-to-Control Communication for complete documentation.

Working with Both Runtimes

Plugins using the unified SDK can work in both Gateway and Studio:
Expandable