Skip to main content

Overview

Free LLM API resources exist because providers want developers to experiment and build amazing things. Following these best practices ensures these services remain available for everyone.
Don’t abuse these services. Excessive usage, automation attacks, or violating terms of service can result in these free tiers being shut down for everyone.

General Principles

Respect Rate Limits

Never try to circumvent or work around rate limits with multiple accounts or IPs

Use Appropriate Models

Choose the smallest model that can accomplish your task effectively

Cache Aggressively

Store responses for identical requests to minimize redundant API calls

Monitor Usage

Track your consumption to stay well within limits and plan accordingly

Security Best Practices

1. API Key Management

Never hardcode API keys in your source code:
Use a .env file (and add it to .gitignore):
.env
.gitignore
Use tools like python-dotenv or dotenv (Node.js) to load environment variables from .env files.
Set a schedule to rotate API keys:
  • Monthly rotation for development projects
  • Weekly rotation for production applications
  • Immediate rotation if you suspect a key has been compromised
Most providers allow multiple keys:
Use different keys for different environments:
  • Development: One set of keys
  • Staging: Different keys
  • Production: Separate keys
Benefits:
  • Easier to track usage by environment
  • Compromised dev key doesn’t affect production
  • Can revoke keys without disrupting all environments

2. Input Validation

Always validate and sanitize user inputs:
Prompt injection is a real security concern. Always validate inputs, especially in user-facing applications.

3. Rate Limiting on Your End

Implement application-level rate limiting:

Performance Optimization

1. Response Caching

Cache responses for at least 1 hour for frequently asked questions. For dynamic content, use shorter TTLs (5-15 minutes).

2. Model Selection Strategy

Choose the right model for the task:

3. Streaming for Better UX

Use streaming to show responses as they generate:
Streaming uses the same API quota but provides better perceived performance - users see results immediately.

Cost Optimization

1. Token Efficiency

Minimize token usage without sacrificing quality:
Use shorter system messages:

2. Conversation Management

Trim conversation history intelligently:

3. Multi-Provider Strategy

Use cheaper/faster providers for simple tasks:

Privacy and Compliance

Providers using your data for training (on free tier):
  • Google AI Studio (outside EU/UK/EEA/CH)
  • Mistral La Plateforme (Experiment plan)
Providers NOT using your data:
  • Google AI Studio (EU/UK/EEA/CH regions)
  • Most other providers (check their privacy policy)
If handling sensitive data, always check the provider’s privacy policy and consider paying for a tier with stronger privacy guarantees.
Never send sensitive information through free APIs:
  • Personal identifiable information (PII)
  • Health records (PHI)
  • Financial data (credit cards, SSNs)
  • Passwords or credentials
  • Trade secrets or confidential business data
Sanitize inputs:
Consider data residency requirements:
  • EU data: Use Google AI Studio (EU regions) or Scaleway (France)
  • US data: Most providers are US-based
  • China data: Use Alibaba Cloud (International)
Check provider terms for:
  • Where data is processed
  • Where data is stored
  • How long data is retained

Error Handling

Implement comprehensive error handling:

Monitoring and Logging

Track important metrics:

Testing and Development

Use Mocks in Tests

Don’t hit real APIs in unit tests - use mocked responses

Set Development Limits

Implement stricter limits in development to avoid accidentally exhausting production quotas

Separate API Keys

Use different API keys for dev, staging, and production

Test Failover

Regularly test your multi-provider fallback logic

Ethical Considerations

These free services exist to help developers learn and build. Please use them responsibly:
  • Don’t create multiple accounts to bypass rate limits
  • Don’t use free tiers for commercial production at scale
  • Don’t generate spam, harmful, or illegal content
  • Consider upgrading to paid tiers when your usage grows
  • Report bugs and issues to help improve the services

Checklist

Before deploying your application:
  • API keys stored in environment variables, not code
  • .env files added to .gitignore
  • Input validation implemented
  • Rate limiting on your application layer
  • Response caching for repeated queries
  • Error handling with retries and exponential backoff
  • Logging and monitoring in place
  • Multi-provider fallback configured
  • No PII or sensitive data in prompts
  • Token usage optimized
  • Conversation history managed
  • Usage tracking and alerts configured

Next Steps

Rate Limits Guide

Deep dive into rate limit optimization strategies

Choosing a Provider

Find the best provider for your specific use case