LLM Proxy Configuration Guide
Overview
Terraphim AI supports using proxy services for LLM providers, including the z.ai proxy for Anthropic models. This guide explains how to configure and use LLM proxies in your Terraphim deployment.
Supported Proxy Providers
z.ai Proxy for Anthropic Models
The z.ai proxy provides an alternative endpoint for Anthropic Claude models with enhanced performance and reliability.
Environment Variables:
# Base URL for the z.ai proxy
ANTHROPIC_BASE_URL=https://api.z.ai/api/anthropic
# Authentication token for z.ai proxy
ANTHROPIC_AUTH_TOKEN=your-z-ai-auth-token
# Optional: Fallback direct Anthropic API key
ANTHROPIC_API_KEY=your-anthropic-api-keyOpenRouter Proxy
You can configure a custom proxy for OpenRouter requests.
Environment Variables:
# Custom OpenRouter proxy endpoint
OPENROUTER_BASE_URL=https://your-proxy.example.com/api/v1
# OpenRouter API key
OPENROUTER_API_KEY=your-openrouter-api-keyOllama Custom Endpoint
Configure Ollama to use a custom endpoint instead of the default local instance.
Environment Variables:
# Custom Ollama endpoint
OLLAMA_BASE_URL=http://your-ollama-server:11434
# Optional: Custom model name
OLLAMA_MODEL_NAME=llama3.1Configuration Methods
Method 1: Environment Variables (Recommended)
Set the environment variables directly in your shell or configuration:
Method 2: 1Password Integration
Use Terraphim's 1Password integration for secure credential management:
# Generate configuration from template
# Source the environment
# Run Terraphim
Method 3: Configuration File
Create a role configuration file with proxy settings:
Usage Examples
Basic Proxy Usage
use ;
async Custom Proxy Configuration
use ;
use Duration;
async OpenRouter Service with z.ai Proxy
use OpenRouterService;
async Troubleshooting
Common Issues
1. Proxy Not Detected
Problem: The service is not using the configured proxy.
Solution:
- Verify environment variables are set correctly
- Check that the model name matches the expected pattern
- Ensure the provider name is correct
# Check environment variables
|
# Test proxy detection
2. Authentication Failures
Problem: Requests are failing with authentication errors.
Solution:
- Verify the auth token is correct and active
- Check that
ANTHROPIC_AUTH_TOKENis set when using z.ai proxy - Ensure the token has the required permissions
# Test authentication with curl
3. Connection Timeouts
Problem: Requests to the proxy are timing out.
Solution:
- Check network connectivity to the proxy endpoint
- Verify firewall rules allow connections to the proxy
- Increase timeout values in configuration
let config = new
.with_timeout; // Increase timeout4. Fallback Not Working
Problem: Fallback to direct endpoints is not working when proxy fails.
Solution:
- Ensure fallback is enabled in configuration
- Verify direct endpoint credentials are available
- Check logs for fallback attempts
let config = new
.with_fallback; // Enable fallbackDebug Mode
Enable debug logging to troubleshoot proxy issues:
# Set log level
# Run with logging
RUST_LOG=debug Testing Proxy Configuration
Use the built-in testing utilities:
# Test proxy connectivity
# Test OpenRouter proxy integration
# Test all proxy functionality
Performance Considerations
Proxy vs Direct Access
- z.ai Proxy: Generally offers better performance and reliability for Anthropic models
- Direct Access: Use as fallback when proxy is unavailable
- Latency: Proxy may add small latency overhead but provides better throughput
Timeout Configuration
Configure appropriate timeouts based on your use case:
// Fast responses (chat completion)
let fast_config = new
.with_timeout;
// Longer processing (document summarization)
let slow_config = new
.with_timeout;Connection Pooling
The proxy client automatically handles connection pooling and reuse for optimal performance.
Security Considerations
Credential Management
- Use 1Password integration for secure credential storage
- Never hardcode API keys in source code
- Rotate auth tokens regularly
- Use environment-specific configurations
Network Security
- Ensure TLS/SSL is used for all proxy connections
- Verify proxy endpoint certificates
- Consider using VPN for additional security
Access Control
- Limit proxy access to authorized applications
- Use service accounts with minimal required permissions
- Monitor proxy usage and access logs
Advanced Configuration
Multiple Proxy Endpoints
Configure different proxies for different providers:
let mut client = new?;
// z.ai proxy for Anthropic
let anthropic_config = new
.with_base_url
.with_api_key;
// Custom proxy for OpenRouter
let openrouter_config = new
.with_base_url
.with_api_key;
client.configure;
client.configure;Load Balancing
For high-availability deployments, configure multiple proxy endpoints:
// Implement load balancing logic
let proxy_urls = vec!;
let selected_url = proxy_urls;Monitoring and Metrics
Track proxy performance and reliability:
// Log proxy usage
info!;
// Monitor response times
let start = now;
let response = make_request.await?;
let duration = start.elapsed;
debug!;Migration Guide
From Direct API Access
- Backup Configuration: Save your current configuration
- Get Proxy Credentials: Obtain z.ai proxy credentials
- Update Environment Variables: Set
ANTHROPIC_BASE_URLandANTHROPIC_AUTH_TOKEN - Test Configuration: Run connectivity tests
- Update Deployments: Apply changes to production
From Other Proxy Services
- Export Current Configuration: Note current proxy settings
- Update URLs: Change base URLs to z.ai endpoints
- Update Authentication: Switch to z.ai auth tokens
- Validate Functionality: Test all LLM operations
- Monitor Performance: Compare with previous proxy service
Support
For issues with LLM proxy configuration:
- Check this documentation for common solutions
- Review the troubleshooting section
- Test with the provided test utilities
- Check the Terraphim AI documentation
- Contact support with specific error messages and configuration details