Skip to content

[recipes/typed-edge-classifier] Add OpenRouter provider support#271

Merged
justfinethanku merged 3 commits into
NateBJones-Projects:mainfrom
sahwan11:contrib/sahwan11/typed-edge-classifier-openrouter
Jun 1, 2026
Merged

[recipes/typed-edge-classifier] Add OpenRouter provider support#271
justfinethanku merged 3 commits into
NateBJones-Projects:mainfrom
sahwan11:contrib/sahwan11/typed-edge-classifier-openrouter

Conversation

@sahwan11

@sahwan11 sahwan11 commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

typed-edge-classifier was the only OB1 recipe that hard-required ANTHROPIC_API_KEY specifically. entity-extraction-worker, wiki-synthesis, and others already accept OPENROUTER_API_KEY (or generic LLM_API_KEY) and prefer OpenRouter when both are set.

This patch closes that gap so a single OPENROUTER_API_KEY can 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

  • loadEnv now requires either OPENROUTER_API_KEY or ANTHROPIC_API_KEY (preferring OpenRouter when both are set, matching entity-extraction-worker's priority order).
  • callAnthropicOnce becomes a thin provider router that dispatches to callOpenRouterOnce (OpenAI-flavored chat completions) or callAnthropicDirectOnce (the original code path, unchanged).
  • resolveModel auto-prefixes bare Anthropic model names (claude-haiku-4-5-20251001, claude-opus-4-7) with anthropic/ when routing through OpenRouter, so default model strings keep working without making operators learn the routing prefix. Already-prefixed strings pass through.
  • normalizeModelForPricing strips the anthropic/ prefix for PRICING lookups 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: prerequisites list, credential tracker, env var setup step, and the Missing env vars troubleshooting entry now reflect both providers.
  • One log line ([classify-edges] LLM ... 429: retry ...) replaces the previous Anthropic-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 original callAnthropicOnce. Only the entry point branches.

Test plan

  • Dry-run 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
    
    Result: 3 candidate pairs sampled, 3 LLM calls succeeded, ~$0.007 spend, 3 below_confidence (correct default behavior at 0.75), grounded rationales returned.
  • loadEnv rejects when neither key is set with a clear error message naming both options.
  • loadEnv accepts when only ANTHROPIC_API_KEY is set (back-compat preserved).
  • loadEnv accepts when only OPENROUTER_API_KEY is set (new path).
  • Hybrid run (Haiku filter + Opus classify) via OpenRouter — not yet tested at scale; expected to work since both legs share the same callAnthropicOnce router.
  • Anthropic-direct path post-patch — visually identical to pre-patch flow; not retested but the diff is purely additive in the new branch.

Notes

  • Did not add .env.local auto-loading to this script (other OB1 scripts that do load .env.local use a per-script helper). Operators still need to export env vars or use dotenv -- node classify-edges.mjs .... Happy to add a loader if maintainers prefer.
  • Did not change any model defaults. Hybrid-mode default (Haiku filter + Opus classify) is preserved — that's the right cost/quality split per the recipe's own README.

🤖 Generated with Claude Code

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>
@github-actions github-actions Bot added the recipe Contribution: step-by-step recipe label May 7, 2026
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown

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.

@alanshurafa alanshurafa added area: recipes Review area: recipes review: ready-for-maintainer Community reviewer recommends maintainer review alan-reviewed Reviewed by Alan Shurafa in Community Reviewer role labels May 20, 2026
@alanshurafa

Copy link
Copy Markdown
Collaborator

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 anthropic/ for OpenRouter routing, an OpenRouter-wins priority order matching entity-extraction-worker, and 429 retry with exponential backoff. The README is clear. Recommend maintainer review.

— Alan (community reviewer; non-binding)

@justfinethanku justfinethanku left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@justfinethanku justfinethanku merged commit f16479e into NateBJones-Projects:main Jun 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

alan-reviewed Reviewed by Alan Shurafa in Community Reviewer role area: recipes Review area: recipes recipe Contribution: step-by-step recipe review: ready-for-maintainer Community reviewer recommends maintainer review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants