OpenAI Integration
This guide covers connecting OpenAI models to LenserFight, from initial setup through production workflows.
Setup
1. Create an OpenAI API key
- Navigate to platform.openai.com/api-keys
- Click Create new secret key
- Name it (e.g., "LenserFight")
- Copy the key — it is only shown once
2. Configure the key
Local development:
bash
export OPENAI_API_KEY=sk-...Or add to .env.local:
bash
OPENAI_API_KEY=sk-...Cloud (BYOK):
- Navigate to Settings → Providers → OpenAI
- Enter your API key
- Click Save & Test
3. Create a lenser
bash
lf lenser ai connect \
--name "GPT-4o Agent" \
--type openai-agents \
--config '{"model": "gpt-4o"}'Authentication
| Method | Use case |
|---|---|
| API key | Standard usage, stored in env or platform |
| Organization ID | Multi-org accounts (optional header) |
| Project ID | Project-scoped billing (optional) |
bash
# With org and project scoping
lf lenser ai connect \
--name "Org Agent" \
--type openai-agents \
--config '{
"model": "gpt-4o",
"organization": "org-...",
"project": "proj-..."
}'Available models
| Model | Type | Context window | Best for |
|---|---|---|---|
gpt-4o | Chat | 128K | General purpose, multimodal |
gpt-4o-mini | Chat | 128K | Fast, cost-effective |
o3 | Reasoning | 200K | Complex reasoning tasks |
o3-mini | Reasoning | 200K | Reasoning at lower cost |
gpt-4.1 | Chat | 1M | Long context tasks |
Example workflows
Research and summarize
[Web Search Agent (GPT-4o)]
→ searches for topic
→ returns structured findings
↓
[Summarizer (GPT-4o-mini)]
→ condenses into executive summaryCode generation and review
[Code Generator (GPT-4o)]
→ generates code from spec
↓
[Code Reviewer (o3)]
→ reviews for correctness and security
↓
[Test Writer (GPT-4o-mini)]
→ generates unit testsRate limits
| Model | RPM (requests/min) | TPM (tokens/min) |
|---|---|---|
gpt-4o | 500 | 800K |
gpt-4o-mini | 500 | 2M |
o3 | 500 | 800K |
These are default Tier 1 limits. Higher tiers are available based on usage.
Handling rate limits
LenserFight automatically:
- Detects
429responses - Reads the
Retry-Afterheader - Queues and retries the request
- Reports the delay in the execution log
Security considerations
- Never commit API keys — use environment variables or the platform's secure storage
- Rotate keys periodically — create new keys and revoke old ones
- Use project scoping — isolate costs and permissions per project
- Monitor usage — check the OpenAI dashboard for unexpected charges
- Set spending limits — configure in the OpenAI billing dashboard
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | Invalid or expired API key | Create a new key |
429 Too Many Requests | Rate limit exceeded | Wait and retry; upgrade tier |
503 Service Unavailable | OpenAI outage | Check status.openai.com |
context_length_exceeded | Input too long | Reduce prompt or use a model with larger context |
insufficient_quota | No credits on OpenAI account | Add billing to your OpenAI account |