Skip to content

feat(llm): add OpenRouter provider — unified API for hundreds of LLM models [$500]#472

Open
daletyler1737 wants to merge 4 commits intoSpectral-Finance:mainfrom
daletyler1737:open-router-provider
Open

feat(llm): add OpenRouter provider — unified API for hundreds of LLM models [$500]#472
daletyler1737 wants to merge 4 commits intoSpectral-Finance:mainfrom
daletyler1737:open-router-provider

Conversation

@daletyler1737
Copy link
Copy Markdown

Problem Summary

Issue #95 requests an OpenRouter API integration to provide access to hundreds of LLM models through a single unified API with automatic cost optimization and model selection.

Solution Approach

Implemented Lux.LLM.OpenRouter following the existing provider pattern:

New Module: lux/lib/lux/llm/open_router.ex

  • Implements Lux.LLM behaviour (the standard provider interface)
  • Unified API endpoint: https://openrouter.ai/api/v1/chat/completions
  • Supports 6+ popular models via provider/model format:
    • anthropic/claude-3.5-sonnet — Best overall quality
    • openai/gpt-4o — OpenAI's flagship
    • openai/gpt-4o-mini — Fast and affordable
    • google/gemini-pro — Google's best model
    • meta-llama/llama-3-70b-instruct — Open-source powerhouse
    • mistralai/mistral-7b-instruct — Efficient and fast
  • Tool/function calling support (Beams, Prisms, Lenses)
  • Automatic retries with configurable count
  • Streaming support
  • JSON mode via response_format
  • HTTP-Referer and X-OpenRouter-Title headers for rankings
  • Cost tracking per model (integrates with ProviderRegistry)

Updated: lux/lib/lux/llm/provider_registry.ex

  • OpenRouter registered as 6th default provider
  • Per-model pricing: from $0.00015/kToken input (GPT-4o-mini)
  • Features: :streaming, :tools, :json_mode
  • Priority 5 (after Ollama local models)

New Test: lux/test/llm/open_router_test.exs

  • Tests provider registration
  • Tests Config struct defaults
  • Tests API key validation error handling
  • Tests ProviderRegistry integration

Acceptance Criteria

  • Implement OpenRouter API client
  • Add support for multiple models
  • Create unified interface matching other providers
  • Implement proper error handling and retries
  • Add cost tracking and reporting
  • Include comprehensive documentation
  • Add integration tests

References

…models

- Lux.LLM.OpenRouter module implementing Lux.LLM behaviour
- OpenRouter API integration with tool/function calling support
- Supports 6+ popular models (Claude, GPT-4o, Gemini, Llama, Mistral)
- Automatic retries, streaming support, JSON mode
- OpenRouter registered in ProviderRegistry with cost tracking
- ExUnit tests included
- Closes Spectral-Finance#95 ($500 bounty)
@daletyler1737 daletyler1737 mentioned this pull request Mar 29, 2026
7 tasks
Corrected open_router.ex to match TogetherAI provider pattern exactly.
Brace/paren balance verified: 88/88 and 123/123 OK.
…n_mode

Bug fixes:
- Remove dangerous retry logic that re-called `call/3` with empty prompt
- Only retry on server errors (5xx) and rate limits (429), not all statuses
- Add proper parallel tool execution via Task.async/await_many
- Add json_mode support via response_format parameter
- Fix Config struct merge order (defaults first, user config second)
- Fix nil/empty header filtering
- Fix execute_tool_calls nil handling and error accumulation
- Add explicit nil/error branches to handle_response
- Fix schema fallback for Beam/Prism/Lens when description is nil

Closes Spectral-Finance#95
- Add 148-line comprehensive test suite covering:
  - Registration, config defaults, override, provider registry integration
  - Tool-to-function conversion for Beam, Prism, Lens
  - json_mode support, enable/disable, cost info
- Add .devcontainer/devcontainer.json for VS Code DevContainers
  - Uses Elixir 1.20 image with GitHub CLI pre-installed
  - Auto-installs mix deps on container creation
  - ElixirLS + Phoenix snippets + Credo extensions

Closes Spectral-Finance#95
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.

1 participant