Skip to content

OpenAI Integration

This guide covers connecting OpenAI models to LenserFight, from initial setup through production workflows.

Setup

1. Create an OpenAI API key

  1. Navigate to platform.openai.com/api-keys
  2. Click Create new secret key
  3. Name it (e.g., "LenserFight")
  4. 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):

  1. Navigate to Settings → Providers → OpenAI
  2. Enter your API key
  3. Click Save & Test

3. Create a lenser

bash
lf lenser ai connect \
  --name "GPT-4o Agent" \
  --type openai-agents \
  --config '{"model": "gpt-4o"}'

Authentication

MethodUse case
API keyStandard usage, stored in env or platform
Organization IDMulti-org accounts (optional header)
Project IDProject-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

ModelTypeContext windowBest for
gpt-4oChat128KGeneral purpose, multimodal
gpt-4o-miniChat128KFast, cost-effective
o3Reasoning200KComplex reasoning tasks
o3-miniReasoning200KReasoning at lower cost
gpt-4.1Chat1MLong 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 summary

Code 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 tests

Rate limits

ModelRPM (requests/min)TPM (tokens/min)
gpt-4o500800K
gpt-4o-mini5002M
o3500800K

These are default Tier 1 limits. Higher tiers are available based on usage.

Handling rate limits

LenserFight automatically:

  1. Detects 429 responses
  2. Reads the Retry-After header
  3. Queues and retries the request
  4. Reports the delay in the execution log

Security considerations

  1. Never commit API keys — use environment variables or the platform's secure storage
  2. Rotate keys periodically — create new keys and revoke old ones
  3. Use project scoping — isolate costs and permissions per project
  4. Monitor usage — check the OpenAI dashboard for unexpected charges
  5. Set spending limits — configure in the OpenAI billing dashboard

Troubleshooting

ErrorCauseFix
401 UnauthorizedInvalid or expired API keyCreate a new key
429 Too Many RequestsRate limit exceededWait and retry; upgrade tier
503 Service UnavailableOpenAI outageCheck status.openai.com
context_length_exceededInput too longReduce prompt or use a model with larger context
insufficient_quotaNo credits on OpenAI accountAdd billing to your OpenAI account

Next steps