Overview
Cloud-hosted models like Gemini and OpenAI require API keys for authentication. This guide covers all methods for setting up and managing your API keys.On-device models and local LLMs (like Ollama) don’t require API keys. See the Model Providers guide for local model setup.
Getting API Keys
Obtain API keys from these providers:- AI Studio: For Gemini models (free tier available)
- Vertex AI: For enterprise Gemini use with service accounts
- OpenAI Platform: For OpenAI models (GPT-4, GPT-3.5)
Configuration Methods
LangExtract supports four methods for providing API keys, listed from most to least recommended for production use.Option 1: Environment Variable (Recommended)
Set theLANGEXTRACT_API_KEY environment variable:
Option 2: .env File (Recommended for Development)
Create a.env file in your project root to keep API keys secure and out of version control.
Option 3: Direct API Key (Not Recommended for Production)
Provide the API key directly in your code:Option 4: Vertex AI (Service Accounts)
For enterprise deployments, use Vertex AI with service account authentication:- Application Default Credentials (ADC)
- Service account JSON key file
- Workload Identity (for GKE)
Vertex AI authentication doesn’t require an API key in the traditional sense. Instead, it uses Google Cloud authentication mechanisms. See the Vertex AI documentation for setup details.
Provider-Specific Keys
Gemini (AI Studio)
Using the defaultLANGEXTRACT_API_KEY:
OpenAI
UsingOPENAI_API_KEY:
OpenAI models require
fence_output=True and use_schema_constraints=False.Docker Configuration
Pass API keys to Docker containers using environment variables:.env file:
Security Best Practices
Never Commit API Keys
- Add
.envto.gitignore - Use environment variables for production
- Rotate keys regularly
Restrict API Key Permissions
- Use least-privilege access
- Set API key restrictions (IP allowlists, referrer restrictions)
- Monitor API key usage
Use Separate Keys for Environments
- Development keys for testing
- Production keys for live deployments
- Different keys per team member when possible
Troubleshooting
No API Key Found
If you see an error about missing API keys:- Check that
LANGEXTRACT_API_KEYis set:echo $LANGEXTRACT_API_KEY - Verify your
.envfile exists and contains the key - Ensure the environment variable is exported (not just set)
- Restart your IDE/terminal after setting environment variables
Invalid API Key
If authentication fails:- Verify the API key is correct (no extra spaces)
- Check that the API key is enabled in the provider’s console
- Ensure the API key has the necessary permissions
- Try generating a new API key
Rate Limits
If you hit rate limits:- Reduce
max_workersto slow down concurrent requests - Upgrade to a higher tier (e.g., Tier 2 for Gemini)
- Add retry logic with exponential backoff
- Monitor your quota usage
For large-scale or production use, a Tier 2 Gemini quota is suggested to increase throughput and avoid rate limits. See the rate-limit documentation for details.
Cost Management
API keys are tied to billing accounts. Monitor and control costs:Cost Factors
- Token volume: Most APIs charge by tokens processed
- Model selection: Different models have different costs
- extraction_passes: Multiple passes reprocess tokens (e.g., 3 passes = 3x cost)
- max_char_buffer: Smaller values = more API calls
Cost Optimization Tips
- Test with small samples: Estimate costs before running on full datasets
- Use appropriate models:
gemini-2.5-flashis more economical thangemini-2.5-pro - Optimize passes: Start with
extraction_passes=1and increase only if needed - Use batch processing: Enable Vertex AI batch API for large-scale tasks
- Monitor usage: Set up billing alerts in your provider’s console
max_workers improves processing speed without increasing token costs—it only affects parallelization.Next Steps
- Learn about model providers and which to choose
- Start with basic extraction to test your API key
- Process long documents efficiently