Overview
Sistema Financiero uses OpenRouter to provide AI-powered features like natural language transaction entry and OCR receipt scanning. This guide will help you configure the necessary API keys and customize AI settings.OpenRouter provides access to multiple AI models through a single API, including Google’s Gemini, OpenAI’s GPT, and many others.
Quick Setup
Get OpenRouter API Key
- Go to openrouter.ai
- Sign up or log in
- Navigate to API Keys
- Click “Create Key”
- Copy your API key (starts with
sk-or-v1-...)
Environment Variables
Sistema Financiero requires these environment variables for AI features:Required
Optional
.env.example in the repository for the complete template.
Model Configuration
Sistema Financiero currently uses Google Gemini 2.5 Flash for both chat and OCR features.Current Model
- Fast response times (< 2 seconds)
- Strong function calling support
- Excellent vision/OCR capabilities
- Cost-effective pricing
- Native Spanish language support
Supported Features
| Feature | Model Used | Capabilities |
|---|---|---|
| AI Chat | Gemini 2.5 Flash | Function calling, conversation history |
| OCR Scanning | Gemini 2.5 Flash | Vision, JSON mode, text extraction |
Customizing the Model
You can switch to different models by editing the API routes:Chat Model
Edit/app/api/chat/route.ts:86:
OCR Model
Edit/app/api/upload-image/route.ts:49:
Alternative Models
Here are some alternative models available on OpenRouter:For Chat (Function Calling)
For OCR (Vision)
API Configuration
Headers
All OpenRouter requests include these headers:- Authorization: Your API key
- HTTP-Referer: Your site URL (for analytics)
- X-Title: App name (appears in OpenRouter dashboard)
Chat Parameters
/app/api/chat/route.ts:84-162
OCR Parameters
/app/api/upload-image/route.ts:48-128
Parameter Tuning
Temperature
- Chat:
0.7- More creative, conversational responses - OCR:
0.1- More deterministic, accurate data extraction
Max Tokens
- Chat:
1000- Enough for conversation + function call - OCR:
400- Sufficient for structured JSON response
Cost Optimization
Strategies
-
Use cheaper models for simple tasks:
- Consider GPT-3.5 Turbo instead of GPT-4 for basic chat
- Gemini 2.5 Flash is already cost-effective
-
Limit conversation history:
-
Reduce max_tokens:
- Set lower limits when possible
- OCR responses don’t need many tokens
-
Monitor usage:
- Check OpenRouter dashboard for usage analytics
- Track
data.usagereturned in API responses
Usage Tracking
The chat API returns usage information:Security Best Practices
Environment File Security
-
Add to
.gitignore: -
Use
.env.exampletemplate: -
Production deployment:
- Use platform’s secret management (Vercel, Railway, etc.)
- Never expose keys in client-side code
API Key Permissions
OpenRouter allows you to set spending limits:- Go to OpenRouter Settings
- Set monthly spending limits
- Enable usage notifications
- Monitor usage regularly
Vercel Deployment
To deploy to Vercel with AI features:Add environment variables
In your Vercel project settings:
- Go to Settings → Environment Variables
- Add
OPENROUTER_API_KEY - Add other required variables
Troubleshooting
”OpenRouter error: 401 Unauthorized”
Cause: Invalid or missing API key Solution:- Check
.env.localhas correct key - Verify key starts with
sk-or-v1- - Restart dev server after adding key
”Error al procesar tu mensaje”
Cause: API request failed Solution:- Check OpenRouter dashboard for errors
- Verify you have sufficient credits
- Check network connectivity
”Function calling not working”
Cause: Model doesn’t support function calling Solution:- Ensure using compatible model (Gemini, GPT-4, Claude 3)
- Check
toolsparameter is correctly formatted - Verify
tool_choice: 'auto'is set
”OCR extraction inaccurate”
Cause: Poor image quality or wrong model Solution:- Use vision-capable model
- Improve image quality (lighting, focus)
- Lower temperature for more consistent results
- Adjust OCR prompt for better extraction
Advanced Configuration
Custom System Prompts
Modify the system prompt in/app/api/chat/route.ts:33-57:
Custom Function Definitions
Add new functions in/app/api/chat/route.ts:89-159:
Streaming Responses
For real-time streaming, there’s also a streaming endpoint:Monitoring
OpenRouter Dashboard
Monitor your usage at openrouter.ai/activity:- Request count
- Token usage
- Cost breakdown
- Model performance
Logs
Check server logs for errors:Resources
OpenRouter Docs
Official API documentation
Model List
Browse available models and pricing
API Keys
Manage your API keys
Usage Dashboard
Monitor API usage and costs
Next Steps
AI Chat
Learn how to use the AI chat assistant
OCR Scanning
Upload receipts for automatic data extraction