[recipes/typed-edge-classifier] Add OpenRouter provider support#271
Conversation
The classifier was the only OB1 recipe that hard-required ANTHROPIC_API_KEY
specifically — entity-extraction-worker, wiki-synthesis, and others already
support OpenRouter (or generic LLM_API_KEY) and prefer it. This patch closes
that gap so a single OPENROUTER_API_KEY can serve every recipe.
Changes:
- loadEnv accepts OPENROUTER_API_KEY OR ANTHROPIC_API_KEY (preferring the
former when both are set, matching entity-extraction-worker priority).
- callAnthropicOnce becomes a provider router that calls either
callOpenRouterOnce (OpenAI-flavored chat completions) or
callAnthropicDirectOnce (the original code path, untouched).
- resolveModel auto-prefixes bare Anthropic model names with "anthropic/"
when routing through OpenRouter, so default model strings keep working
without forcing operators to know the routing prefix.
- normalizeModelForPricing strips the "anthropic/" prefix for PRICING
lookup so pre-flight cost cap arithmetic still finds existing rows.
PRICING values are unchanged — OpenRouter passes Anthropic's per-token
rates through with only a small surcharge, well within --max-cost-usd
soft-cap precision.
- README updated: prereq list, credential tracker, env var step, and the
"Missing env vars" troubleshooting entry now reflect both providers.
- Retry log line now says "LLM" instead of "Anthropic" so OpenRouter runs
don't show misleading provider in retries.
Backward compatibility: existing ANTHROPIC_API_KEY-only deployments keep
working unchanged. The Anthropic-direct code path is preserved verbatim.
Tested locally on a 127-thought OpenBrain via OpenRouter:
node classify-edges.mjs --dry-run --limit 3 --no-hybrid \
--model claude-haiku-4-5 --no-cost-cap
-> 3 candidate pairs sampled, 3 LLM calls succeeded, ~$0.007 spend,
3 below_confidence (correct), grounded rationales returned.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Hey @sahwan11 — welcome to Open Brain Source! 👋 Thanks for submitting your first PR. The automated review will run shortly and check things like metadata, folder structure, and README completeness. If anything needs fixing, the review comment will tell you exactly what. Once the automated checks pass, a human admin will review for quality and clarity. Expect a response within a few days. If you have questions, check out CONTRIBUTING.md or open an issue. |
|
Thanks for the contribution, and welcome. Clean provider-add — OpenRouter alongside the existing Anthropic-direct path, backwards-compatible, and the details are handled well: auto-prefixing Anthropic model names with — Alan (community reviewer; non-binding) |
justfinethanku
left a comment
There was a problem hiding this comment.
Maintainer review: Alan's triage calls this a clean backwards-compatible OpenRouter provider add. I updated the branch, added the repo community badge/creator credit convention, and ran markdownlint locally with 0 errors. GitHub label passed; the Markdown Lint workflow did not report as a PR check after the fork update.
Summary
typed-edge-classifierwas the only OB1 recipe that hard-requiredANTHROPIC_API_KEYspecifically.entity-extraction-worker,wiki-synthesis, and others already acceptOPENROUTER_API_KEY(or genericLLM_API_KEY) and prefer OpenRouter when both are set.This patch closes that gap so a single
OPENROUTER_API_KEYcan serve every OB1 recipe in a deployment, without forcing operators to maintain a separate Anthropic-direct billing account just for one recipe in the wiki-compiler chain.What changed
loadEnvnow requires eitherOPENROUTER_API_KEYorANTHROPIC_API_KEY(preferring OpenRouter when both are set, matchingentity-extraction-worker's priority order).callAnthropicOncebecomes a thin provider router that dispatches tocallOpenRouterOnce(OpenAI-flavored chat completions) orcallAnthropicDirectOnce(the original code path, unchanged).resolveModelauto-prefixes bare Anthropic model names (claude-haiku-4-5-20251001,claude-opus-4-7) withanthropic/when routing through OpenRouter, so default model strings keep working without making operators learn the routing prefix. Already-prefixed strings pass through.normalizeModelForPricingstrips theanthropic/prefix forPRICINGlookups so pre-flight cost-cap arithmetic still finds existing rows. Pricing values are unchanged — OpenRouter passes Anthropic's per-token rates through with only a small surcharge, well within--max-cost-usdsoft-cap precision.Missing env varstroubleshooting entry now reflect both providers.[classify-edges] LLM ... 429: retry ...) replaces the previousAnthropic-only label so OpenRouter runs don't show a misleading provider name on retries.Backward compatibility
Existing
ANTHROPIC_API_KEY-only deployments keep working unchanged. The Anthropic-direct code path (callAnthropicDirectOnce) is preserved verbatim from the originalcallAnthropicOnce. Only the entry point branches.Test plan
below_confidence(correct default behavior at 0.75), grounded rationales returned.loadEnvrejects when neither key is set with a clear error message naming both options.loadEnvaccepts when onlyANTHROPIC_API_KEYis set (back-compat preserved).loadEnvaccepts when onlyOPENROUTER_API_KEYis set (new path).callAnthropicOncerouter.Notes
.env.localauto-loading to this script (other OB1 scripts that do load.env.localuse a per-script helper). Operators still need toexportenv vars or usedotenv -- node classify-edges.mjs .... Happy to add a loader if maintainers prefer.🤖 Generated with Claude Code