Skip to content

feat: add OpenRouter provider for credit/usage monitoring#44

Merged
prakersh merged 1 commit intoonllm-dev:mainfrom
omnissiah-comelse:feat/openrouter-provider
Mar 29, 2026
Merged

feat: add OpenRouter provider for credit/usage monitoring#44
prakersh merged 1 commit intoonllm-dev:mainfrom
omnissiah-comelse:feat/openrouter-provider

Conversation

@omnissiah-comelse
Copy link
Copy Markdown
Contributor

Summary

Adds OpenRouter as a new provider for tracking API credit usage, spending rates, and rate limits.

What it does

Polls GET /api/v1/auth/key to track:

  • Total usage (USD cumulative)
  • Daily/weekly/monthly usage breakdowns
  • Credit limit and remaining balance (when a spending limit is configured)
  • Rate limit info (requests per interval)
  • Free tier detection

Why

OpenRouter is widely used by developers running multi-model setups (Claude, GPT, Llama, Mistral, etc.) through a single API. There's currently no way to monitor OpenRouter quota/spending in onWatch — this fills that gap.

Implementation

Follows the existing provider pattern (Z.ai / MiniMax):

File Purpose
internal/api/openrouter_client.go HTTP client with Bearer auth
internal/api/openrouter_types.go Response structs, snapshot type
internal/store/openrouter_store.go SQLite storage + cycle tracking
internal/tracker/openrouter_tracker.go Delta tracking, rate projections
internal/agent/openrouter_agent.go Polling agent

Integration points modified:

  • internal/config/config.goOPENROUTER_API_KEY env var + HasProvider()
  • internal/store/store.go — Schema tables + indexes
  • internal/web/handlers.go — Dashboard endpoints (current, history, cycles, insights, sessions)
  • main.go — Client/tracker/agent wiring + banner display
  • .env.example — New env var entry

Testing

  • go build ./... — clean
  • go vet ./... — clean
  • Tested locally with a real OpenRouter API key — polling works, snapshots stored, cycle tracking functional

Dashboard

The OpenRouter tab shows:

  • Credits usage card (usage vs limit when configured, or total usage)
  • Daily/weekly/monthly usage breakdowns
  • Usage rate projections
  • Historical chart data
  • Session tracking
  • Cycle overview

Configuration

OPENROUTER_API_KEY=sk-or-v1-your-key-here

No auto-detection — requires explicit API key in .env or environment.

Add OpenRouter as a new provider for tracking API credit usage,
daily/weekly/monthly spending, and rate limits.

Polls GET /api/v1/auth/key for usage data including:
- Total usage (USD)
- Daily/weekly/monthly usage breakdowns
- Credit limit and remaining balance (when configured)
- Rate limit info
- Free tier detection

Implementation follows the existing provider pattern (Z.ai/MiniMax):
- internal/api/openrouter_client.go: HTTP client with Bearer auth
- internal/api/openrouter_types.go: Response structs and snapshot type
- internal/store/openrouter_store.go: SQLite storage and cycle tracking
- internal/tracker/openrouter_tracker.go: Delta tracking and projections
- internal/agent/openrouter_agent.go: Polling agent

Integration points:
- Config: OPENROUTER_API_KEY env var
- Dashboard: Provider tab with usage cards and charts
- Auto-detection: Uses env var (no local auth file)

Env var: OPENROUTER_API_KEY
Copy link
Copy Markdown
Contributor

@prakersh prakersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend implementation is solid - follows existing provider patterns, parameterized SQL, proper API key redaction, bounded queries. Frontend needs OpenRouter-specific branches in dashboard.html and app.js - will fix in a follow-up.

@prakersh prakersh merged commit 80308f1 into onllm-dev:main Mar 29, 2026
@prakersh
Copy link
Copy Markdown
Contributor

Thanks for this contribution @omnissiah-comelse! The backend implementation was solid - clean provider pattern, parameterized SQL, proper key redaction. We've added the frontend integration in #48 (Credits quota card, usage charts, cycle overview, and menubar support).

If onWatch has been useful to you, we'd appreciate a star on the repo - it helps others discover the project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants