Keep API keys private
- Call LLM.kiwi from a backend, serverless function, worker, or trusted script.
- Load the key from an environment variable or managed secret store.
- Never commit a key to Git, paste it into an issue, or expose it in browser code.
- Use separate keys for separate applications when practical.
- Revoke a key immediately if it leaks.
Set timeouts
Every network request can stall. Set a finite timeout and show a useful message when it expires.Retry only temporary failures
Use exponential backoff with jitter and a maximum attempt count. Do not run an infinite retry loop.
Validate model output
Generated text can be wrong, incomplete, unsafe, or differently formatted than requested.- Parse JSON inside
try/catchor equivalent. - Check allowed labels against an explicit list.
- Validate required fields, types, ranges, and length.
- Escape or sanitize content before rendering it as HTML.
- Do not execute generated code or commands automatically.
- Require human review for important decisions and public content.
Keep prompts focused
A good instruction states:- The task
- Relevant context
- Constraints
- Desired output
Manage conversation history
The API does not remember previous requests. Your application controls history.- Keep the system instruction at the beginning.
- Include turns needed to understand the current request.
- Remove irrelevant old turns.
- Summarize long history when appropriate.
- Avoid placing secrets or unnecessary personal data in messages.
Protect privacy
Before sending content:- Collect only what the feature needs.
- Avoid transmitting passwords, secret keys, payment details, or sensitive records.
- Redact personal data when possible.
- Define how long application logs and conversation history are retained.
- Inform users when their input is processed by an AI service.
Control cost and capacity
Even when the API is free, capacity is limited.- Cap input size and output length.
- Rate-limit users of your own application.
- Cache safe, reusable results.
- Limit parallel background jobs.
- Cancel requests when the user abandons the operation, if your runtime supports it.
- Track error rate and latency.
Log safely
Useful diagnostic data includes:- Timestamp and duration
- Endpoint and model value
- HTTP status
- Request ID when available
- Attempt count
Production readiness checklist
- Key is stored server-side
- Secrets are excluded from Git and logs
- Request timeout is configured
- Temporary failures use bounded backoff
- Permanent failures are not retried
- User input size is limited
- Model output is validated
- Important actions require deterministic checks or human review
- Users receive understandable error messages
- Monitoring does not expose private content
Build a practical feature
Apply these rules to common use cases.
Handle every error
Add predictable error behavior.