B2ALABS® - Smart API Gateway Platform
®

Guides & Best Practices

Learn how to maximize B2ALABS capabilities with practical guides and tutorials. All guides based on real production deployments and proven techniques.

New to B2ALABS?

Start with our Getting Started guide to set up B2ALABS in under 10 minutes, then explore these guides to optimize your deployment.

Cost Optimization

Beginner10 minutes

Reduce AI Costs by 70%

Use intelligent routing to automatically select the cheapest available provider for each request

💰 Up to 70% cost reduction

  • Automatic cost estimation for all 8 providers
  • Smart routing to cheapest healthy provider
  • Real-time pricing optimization
  • 97% savings: GPT-5 → Gemini-flash-lite
Beginner5 minutes

Model Selection Strategies

Choose the right model tier based on task complexity to optimize cost vs. quality

💰 40-60% on average

  • GPT-5 for complex reasoning ($0.0075/1K)
  • GPT-5-mini for simple tasks ($0.001/1K)
  • Gemini-flash-lite for bulk operations ($0.00015/1K)
  • Cost comparison calculator included

Reliability & Failover

Intermediate15 minutes

Implement Multi-Provider Failover

Set up automatic failover between providers for 99.9% uptime

  • Fallback chains with 8 providers
  • Health checking every 30 seconds
  • Automatic provider recovery
  • Zero downtime during provider outages
Intermediate20 minutes

Health Monitoring & Alerting

Monitor provider health and set up alerts for issues

  • Real-time provider health status
  • Success rate tracking
  • Latency monitoring
  • Automatic unhealthy provider bypass

Security & Access Control

Advanced30 minutes

Security Best Practices

Secure your B2ALABS deployment with industry best practices

  • JWT authentication with 32+ char secrets
  • API key management and rotation
  • HTTPS/TLS configuration
  • Rate limiting and abuse prevention
Intermediate15 minutes

Rate Limiting & Abuse Prevention

Protect your gateway from abuse and control costs

  • Per-user rate limiting with Redis
  • Global rate limits
  • Custom rate limit tiers
  • Automatic blocking of abusers

Advanced Topics

Advanced45 minutes

Advanced Routing Strategies

Implement custom routing logic for specialized use cases

  • 5 routing strategies: Lowest Cost, Lowest Latency, Round Robin, Priority, Load Balanced
  • Custom fallback chains
  • Provider weighting
  • Dynamic strategy switching
Advanced40 minutes

Performance Optimization

Maximize throughput and minimize latency

  • Redis caching strategies
  • Connection pooling optimization
  • Load testing with k6
  • Prometheus metrics and Grafana dashboards

Supported AI Providers (October 2025)

B2ALABS supports 8 major AI providers with 20+ models for intelligent routing and failover

OpenAI

Supported
  • GPT-5
  • GPT-5 Pro
  • GPT-5 Mini
  • GPT-5
  • GPT-5 Mini

Azure OpenAI

Supported
  • All OpenAI models

Anthropic

Supported
  • Claude Opus 4
  • Claude Sonnet 4.5
  • Claude Haiku 3

Google Gemini

Supported
  • Gemini 2.5 Pro
  • Gemini 2.5 Flash
  • Gemini 2.5 Flash-Lite

Mistral AI

Supported
  • Mistral Large
  • Mistral Medium
  • Mixtral 8x7B

xAI Grok

Supported
  • Grok 4

Meta Llama

Supported
  • Llama 4

DeepSeek

Supported
  • DeepSeek R1
  • DeepSeek V3

How to Reduce AI Costs by 70%

Use B2ALABS intelligent routing to automatically select the cheapest provider

Step 1: Enable Cost-Based Routing

Configure B2ALABS to use the lowest_cost routing strategy:

# Environment variable
ROUTING_STRATEGY=lowest_cost

# Or via API request
curl -X POST http://localhost:8080/api/v1/ai/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5",
    "routing_strategy": "lowest_cost",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Step 2: Configure Multiple Providers

Add API keys for multiple providers in your .env file:

# .env file
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=AIza...
AZURE_OPENAI_API_KEY=...
MISTRAL_API_KEY=...

Step 3: Let B2ALABS Choose

B2ALABS will automatically:

  • Estimate prompt tokens (1 token ≈ 4 characters)
  • Calculate costs for all available providers
  • Select the cheapest healthy provider
  • Fallback to next cheapest if first fails

Real Savings Examples:

  • • GPT-5 ($0.005/1K) → Gemini-flash-lite ($0.00015/1K) = 97% savings
  • • GPT-5-mini ($0.001/1K) → Gemini-flash-lite ($0.00015/1K) = 85% savings
  • • Claude Opus 4 ($0.015/1K) → Claude Haiku 3 ($0.00025/1K) = 98% savings

Implement Multi-Provider Failover

Achieve 99.9% uptime with automatic failover between 8 providers

How Failover Works

B2ALABS automatically configures fallback chains for each model. When a request fails:

  1. Primary provider attempts request
  2. If failed, health checker marks provider as potentially unhealthy
  3. Automatic retry with next provider in fallback chain
  4. Continue until success or all providers exhausted
  5. Provider auto-recovers when success rate improves

Example Fallback Chain (GPT-5)

  1. 1Azure OpenAI GPT-5 (Primary)
  2. 2OpenAI GPT-5 (Fallback)
  3. 3Anthropic Claude Opus 4 (Cross-provider)
  4. 4Claude Sonnet 4.5 (Cost-effective)
  5. 5OpenAI GPT-5-mini (Final fallback)

Health Checking Configuration

# Automatic health checking (from smart_router.go)
- CheckIcon interval: 30 seconds
- Failure threshold: 3 consecutive failures
- Auto-recovery: When success rate > 90%

# View provider health
curl http://localhost:8080/api/v1/router/health \
  -H "Authorization: Bearer YOUR_API_KEY"

Result: 99.9% Uptime

With 8 providers and automatic failover, your gateway remains operational even when multiple providers experience outages. Each request has up to 5 fallback options, ensuring high availability.

Need More Help?

Join our community or contact support for personalized guidance on optimizing your B2ALABS deployment.

Was this page helpful?