Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

Switch cloud extraction from Anthropic to OpenRouter

anirudhkumar-nanonets9f6d7a3b949e
diff --git a/.env.example b/.env.example
index 6e71456d..7e439840 100644
--- a/.env.example
+++ b/.env.example
@@ -7,12 +7,16 @@
# are picked up automatically when present.
# --- Cloud (optional) ---------------------------------------------------------
-# Entity/relationship extraction on Claude (Anthropic).
-# ANTHROPIC_API_KEY=sk-ant-...
-# Optional: override the extraction model (default: claude-haiku-4-5-20251001)
-# CONTEXT_GRAPH_EXTRACTION_MODEL=claude-haiku-4-5-20251001
+# Entity/relationship extraction via OpenRouter (https://openrouter.ai).
+# OpenRouter fronts many models with one key; pick any tool-calling model.
+# OPENROUTER_API_KEY=sk-or-...
+# Optional: override the extraction model (default: openai/gpt-4o-mini)
+# CONTEXT_GRAPH_OPENROUTER_MODEL=openai/gpt-4o-mini
+# Optional: override the API base URL (default: https://openrouter.ai/api/v1)
+# OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
-# Embeddings on OpenAI (Anthropic has no embeddings endpoint).
+# Embeddings on OpenAI (optional — OpenRouter has no embeddings endpoint, so
+# embeddings run locally unless this key is set).
# OPENAI_API_KEY=sk-...
# Optional: override the embedding model (default: text-embedding-3-small)
# CONTEXT_GRAPH_EMBEDDING_MODEL=text-embedding-3-small
diff --git a/DEMO.md b/DEMO.md
index ef1727f0..22188b1d 100644
--- a/DEMO.md
+++ b/DEMO.md
@@ -22,7 +22,7 @@ Think "durable, structured memory for a team of agents" — the same integration
| **Retrieval** (`src/retrieval/`) | Semantic match on entities → one-hop graph expansion → supporting passages → a ready-to-inject prompt block | ✅ Done |
| **Storage** (`src/graph/sqlite-store.ts`) | Embedded SQLite (`better-sqlite3`), zero infra; swappable behind a `GraphStore` interface | ✅ Done |
| **Local-first providers** (`src/ai/local.ts`) | In-process embeddings (transformers.js) + local extraction (Ollama) — **runs with no API keys** | ✅ Done |
-| **Cloud auto-upgrade** (`src/engine.ts`) | If `ANTHROPIC_API_KEY` / `OPENAI_API_KEY` are set, uses Claude + OpenAI automatically for higher quality | ✅ Done |
+| **Cloud auto-upgrade** (`src/engine.ts`) | If `OPENROUTER_API_KEY` is set, uses OpenRouter for extraction automatically (any tool-calling model) for higher quality | ✅ Done |
| **Three access modes** | Library, `context-graph` CLI, and `context-graph-mcp` MCP server for any agent | ✅ Done |
| **One-line installer** (`install.sh`) | `curl … | sh` clones, builds, and puts both commands on PATH | ✅ Done |
| Update modes (silent vs. flagged) | Deferred per your call — needs discussion | ⏸ Deferred |
@@ -38,12 +38,12 @@ The engine needs a model for **extraction** (pulling entities/relationships out
**Right now this machine has neither Ollama nor any API key set**, so pick one path first:
-### Path A — Cloud (fastest to a clean demo, best quality) ✅ recommended for a live demo
+### Path A — Cloud via OpenRouter (fastest to a clean demo, best quality) ✅ recommended for a live demo
```bash
-export ANTHROPIC_API_KEY=sk-ant-... # extraction via Claude
-export OPENAI_API_KEY=sk-... # embeddings via OpenAI (optional; local otherwise)
+export OPENROUTER_API_KEY=sk-or-... # extraction (default model: openai/gpt-4o-mini)
+export OPENAI_API_KEY=sk-... # embeddings (optional; embeddings stay local otherwise)
```
-Reliable, fast, produces the richest graph. Best if you want the demo to look sharp.
+Reliable, fast, produces the richest graph. One OpenRouter key covers extraction — embeddings run locally unless you also set an OpenAI key. Best if you want the demo to look sharp.
### Path B — Fully local (the "no keys" story your manager asked for)
```bash
@@ -126,8 +126,8 @@ src/
ai/
providers.ts ← config resolution (constructor → env → defaults) + interfaces
local.ts ← key-free LocalEmbedder + OllamaExtractor
- anthropic.ts ← Claude extraction (tool-calling)
- openai.ts ← OpenAI embeddings
+ openrouter.ts ← cloud extraction via OpenRouter (tool-calling)
+ openai.ts ← OpenAI embeddings (optional)
graph/
types.ts ← the data model (nodes, edges, chunks) — read this to grok the domain
merge.ts ← dedup + reinforcement (the "gets smarter" logic)
diff --git a/README.md b/README.md
index 0729d217..38df2c96 100644
--- a/README.md
+++ b/README.md
@@ -38,11 +38,11 @@ Out of the box the engine needs **no accounts and no cloud calls**:
ollama pull llama3.2
```
-Want higher-quality cloud models instead? Set either key and the engine uses it **automatically** (local remains the fallback):
+Want higher-quality cloud models instead? Set a key and the engine uses it **automatically** (local remains the fallback):
```bash
-export ANTHROPIC_API_KEY=sk-ant-... # cloud extraction (Claude)
-export OPENAI_API_KEY=sk-... # cloud embeddings (OpenAI)
+export OPENROUTER_API_KEY=sk-or-... # cloud extraction via OpenRouter
+export OPENAI_API_KEY=sk-... # cloud embeddings (optional; local otherwise)
```
Force local even when keys are present with `CONTEXT_GRAPH_LOCAL=1`.
@@ -73,7 +73,7 @@ await engine.contribute(
);
```
-Run the full working demo (needs both keys):
+Run the full working demo (set `OPENROUTER_API_KEY`, or run a local Ollama):
```bash
npm run example
@@ -123,7 +123,7 @@ Add to your Claude Code / MCP client config:
}
```
-The API-key env vars are optional — add `ANTHROPIC_API_KEY` / `OPENAI_API_KEY` only if you want cloud models instead of the local defaults.
+The API-key env vars are optional — add `OPENROUTER_API_KEY` (and optionally `OPENAI_API_KEY` for embeddings) only if you want cloud models instead of the local defaults.
Tools exposed:
@@ -157,7 +157,7 @@ This is what makes contributions compound: the tenth agent to confirm a fact str
ingest / contribute read
┌──────────────────────────┐ ┌────────────────────────┐
│ text → chunk → embed │ │ query → embed │
- │ → extract (Claude) │ │ → semantic match nodes │
+ │ → extract (LLM) │ │ → semantic match nodes │
│ → MERGE (dedup + │───────────▶│ → expand 1 hop (edges) │
│ reinforce) │ graph │ → gather source chunks │
└──────────────────────────┘ (SQLite) │ → render context bundle│
@@ -165,7 +165,7 @@ This is what makes contributions compound: the tenth agent to confirm a fact str
```
- **Storage** — SQLite (via `better-sqlite3`) by default; swap in your own `GraphStore`.
-- **Extraction** — local **Ollama** (`llama3.2` by default) using structured JSON output; automatically upgrades to **Claude** (`claude-haiku-4-5`) if `ANTHROPIC_API_KEY` is set.
+- **Extraction** — local **Ollama** (`llama3.2` by default) using structured JSON output; automatically upgrades to **OpenRouter** (`openai/gpt-4o-mini` by default, any tool-calling model) if `OPENROUTER_API_KEY` is set.
- **Embeddings** — local **in-process** model (`Xenova/all-MiniLM-L6-v2`, 384-dim) by default; automatically upgrades to **OpenAI** (`text-embedding-3-small`) if `OPENAI_API_KEY` is set.
- **Retrieval** — semantic match over entities + one-hop graph expansion + supporting source passages.
@@ -181,9 +181,9 @@ Everything is configurable via constructor options, environment variables, or de
new ContextGraphEngine({
dbPath: "./.context-graph/graph.db", // CONTEXT_GRAPH_DB
// Cloud (used automatically when set):
- anthropicApiKey: process.env.ANTHROPIC_API_KEY,
- openaiApiKey: process.env.OPENAI_API_KEY,
- extractionModel: "claude-haiku-4-5-20251001", // CONTEXT_GRAPH_EXTRACTION_MODEL
+ openrouterApiKey: process.env.OPENROUTER_API_KEY,
+ openrouterModel: "openai/gpt-4o-mini", // CONTEXT_GRAPH_OPENROUTER_MODEL
+ openaiApiKey: process.env.OPENAI_API_KEY, // optional, embeddings only
embeddingModel: "text-embedding-3-small", // CONTEXT_GRAPH_EMBEDDING_MODEL
// Local (the zero-key fallback):
forceLocal: false, // CONTEXT_GRAPH_LOCAL=1 forces local
diff --git a/examples/quickstart.ts b/examples/quickstart.ts
index 74608c2c..d2cf2972 100644
--- a/examples/quickstart.ts
+++ b/examples/quickstart.ts
@@ -1,10 +1,12 @@
/**
- * Quickstart — run with real Claude + OpenAI keys:
+ * Quickstart. Runs with either cloud or fully-local providers:
*
- * export ANTHROPIC_API_KEY=sk-ant-...
- * export OPENAI_API_KEY=sk-...
+ * # Cloud extraction via OpenRouter (embeddings stay local unless OPENAI_API_KEY is set):
+ * export OPENROUTER_API_KEY=sk-or-...
* npm run example
*
+ * # Or fully local — no keys — with a running Ollama (`ollama pull llama3.2`).
+ *
* Uses an in-memory graph so it leaves nothing behind.
*/
import "dotenv/config";
diff --git a/install.sh b/install.sh
index 13c8ae21..4d870d2d 100755
--- a/install.sh
+++ b/install.sh
@@ -64,8 +64,8 @@ cat <<'NEXT'
• Extraction defaults to a local Ollama model. Install Ollama and pull one:
https://ollama.com then ollama pull llama3.2
- Prefer higher-quality cloud extraction? Set keys and it's used automatically:
- export ANTHROPIC_API_KEY=sk-ant-... # extraction
+ Prefer higher-quality cloud extraction? Set a key and it's used automatically:
+ export OPENROUTER_API_KEY=sk-or-... # extraction via OpenRouter
export OPENAI_API_KEY=sk-... # embeddings (optional)
Quick start:
diff --git a/package-lock.json b/package-lock.json
index 185e358e..2572e543 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,7 +9,6 @@
"version": "0.1.0",
"license": "MIT",
"dependencies": {
- "@anthropic-ai/sdk": "^0.109.1",
"@huggingface/transformers": "^3.8.1",
"@modelcontextprotocol/sdk": "^1.29.0",
"better-sqlite3": "^12.11.1",
@@ -32,36 +31,6 @@
"node": ">=20"
}
},
- "node_modules/@anthropic-ai/sdk": {
- "version": "0.109.1",
- "resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.109.1.tgz",
- "integrity": "sha512-q9OnEKLr5H9nxSuXdgDgJhxfYMiE+AaUEBze2Gk91UcaaLnsN+Lx5fbCYywiqurU/APLdwv23x03Wm6WN3EBsg==",
- "license": "MIT",
- "dependencies": {
- "json-schema-to-ts": "^3.1.1",
- "standardwebhooks": "^1.0.0"
- },
- "bin": {
- "anthropic-ai-sdk": "bin/cli"
- },
- "peerDependencies": {
- "zod": "^3.25.0 || ^4.0.0"
- },
- "peerDependenciesMeta": {
- "zod": {
- "optional": true
- }
- }
- },
- "node_modules/@babel/runtime": {
- "version": "7.29.7",
- "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.7.tgz",
- "integrity": "sha512-Nq8OhGWiZIZGV6hLHoyAKLLcJihP/xFeBMGJoUrxTX2psI8dCifzLhZISFb+VWS3wFMRDmCGw5R+dOySCqPLhw==",
- "license": "MIT",
- "engines": {
- "node": ">=6.9.0"
- }
- },
"node_modules/@emnapi/runtime": {
"version": "1.11.2",
"resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.2.tgz",
@@ -1121,12 +1090,6 @@
"integrity": "sha512-oOAWABowe8EAbMyWKM0tYDKi8Yaox52D+HWZhAIJqQXbqe0xI/GV7FhLWqlEKreMkfDjshR5FKgi3mnle0h6Eg==",
"license": "BSD-3-Clause"
},
- "node_modules/@stablelib/base64": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/@stablelib/base64/-/base64-1.0.1.tgz",
- "integrity": "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==",
- "license": "MIT"
- },
"node_modules/@types/better-sqlite3": {
"version": "7.6.13",
"resolved": "https://registry.npmjs.org/@types/better-sqlite3/-/better-sqlite3-7.6.13.tgz",
@@ -1789,12 +1752,6 @@
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
"license": "MIT"
},
- "node_modules/fast-sha256": {
- "version": "1.3.0",
- "resolved": "https://registry.npmjs.org/fast-sha256/-/fast-sha256-1.3.0.tgz",
- "integrity": "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==",
- "license": "Unlicense"
- },
"node_modules/fast-uri": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.3.tgz",
@@ -2138,19 +2095,6 @@
"url": "https://github.com/sponsors/panva"
}
},
- "node_modules/json-schema-to-ts": {
- "version": "3.1.1",
- "resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-3.1.1.tgz",
- "integrity": "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==",
- "license": "MIT",
- "dependencies": {
- "@babel/runtime": "^7.18.3",
- "ts-algebra": "^2.0.0"
- },
- "engines": {
- "node": ">=16"
- }
- },
"node_modules/json-schema-traverse": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
@@ -2976,16 +2920,6 @@
"integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==",
"license": "BSD-3-Clause"
},
- "node_modules/standardwebhooks": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/standardwebhooks/-/standardwebhooks-1.0.0.tgz",
- "integrity": "sha512-BbHGOQK9olHPMvQNHWul6MYlrRTAOKn03rOe4A8O3CLWhNf4YHBqq2HJKKC+sfqpxiBY52pNeesD6jIiLDz8jg==",
- "license": "MIT",
- "dependencies": {
- "@stablelib/base64": "^1.0.0",
- "fast-sha256": "^1.3.0"
- }
- },
"node_modules/statuses": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
@@ -3075,12 +3009,6 @@
"node": ">=0.6"
}
},
- "node_modules/ts-algebra": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz",
- "integrity": "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==",
- "license": "MIT"
- },
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
diff --git a/package.json b/package.json
index b94b1119..5a357c61 100644
--- a/package.json
+++ b/package.json
@@ -43,7 +43,6 @@
"node": ">=20"
},
"dependencies": {
- "@anthropic-ai/sdk": "^0.109.1",
"@huggingface/transformers": "^3.8.1",
"@modelcontextprotocol/sdk": "^1.29.0",
"better-sqlite3": "^12.11.1",
diff --git a/src/ai/anthropic.ts b/src/ai/anthropic.ts
deleted file mode 100644
index d200c7e6..00000000
--- a/src/ai/anthropic.ts
+++ /dev/null
@@ -1,111 +0,0 @@
-import Anthropic from "@anthropic-ai/sdk";
-import type { Extractor } from "./providers.js";
-import type { Extraction } from "../graph/types.js";
-
-const EXTRACTION_TOOL: Anthropic.Tool = {
- name: "record_graph",
- description:
- "Record the entities and relationships extracted from the text as a knowledge graph.",
- input_schema: {
- type: "object",
- properties: {
- entities: {
- type: "array",
- description: "Distinct real-world entities or concepts mentioned in the text.",
- items: {
- type: "object",
- properties: {
- name: { type: "string", description: "Canonical, specific name of the entity." },
- type: {
- type: "string",
- description:
- "Coarse category, lowercase, e.g. concept, system, service, api, person, org, policy, tool, event, metric.",
- },
- summary: {
- type: "string",
- description:
- "One or two sentences describing the entity, grounded in the text.",
- },
- aliases: {
- type: "array",
- items: { type: "string" },
- description: "Alternate names/abbreviations used for this entity.",
- },
- },
- required: ["name", "type", "summary"],
- },
- },
- relations: {
- type: "array",
- description: "Directed relationships between the extracted entities.",
- items: {
- type: "object",
- properties: {
- source: { type: "string", description: "Name of the source entity." },
- target: { type: "string", description: "Name of the target entity." },
- relation: {
- type: "string",
- description:
- "Predicate in snake_case, e.g. depends_on, part_of, authenticates_with, produces, owns, replaces.",
- },
- description: {
- type: "string",
- description: "Short explanation of the relationship.",
- },
- },
- required: ["source", "target", "relation"],
- },
- },
- },
- required: ["entities", "relations"],
- },
-};
-
-const SYSTEM_PROMPT = `You are a knowledge-graph extraction engine. Given a passage of text, identify the salient entities/concepts and the relationships between them.
-
-Rules:
-- Prefer specific, canonical entity names over pronouns or vague phrases.
-- Only include relationships where BOTH endpoints are in your entities list.
-- Deduplicate: merge obvious surface-form variants into one entity with aliases.
-- Do not invent facts that are not supported by the text.
-- Keep summaries concise and factual.
-Always respond by calling the record_graph tool.`;
-
-/** Entity/relationship extractor backed by Claude (Anthropic). */
-export class AnthropicExtractor implements Extractor {
- private client: Anthropic;
- private model: string;
-
- constructor(apiKey: string, model: string) {
- this.client = new Anthropic({ apiKey });
- this.model = model;
- }
-
- async extract(text: string, opts?: { hint?: string }): Promise<Extraction> {
- const userContent = opts?.hint
- ? `Context hint: ${opts.hint}\n\nText:\n${text}`
- : `Text:\n${text}`;
-
- const response = await this.client.messages.create({
- model: this.model,
- max_tokens: 4096,
- system: SYSTEM_PROMPT,
- tools: [EXTRACTION_TOOL],
- tool_choice: { type: "tool", name: "record_graph" },
- messages: [{ role: "user", content: userContent }],
- });
-
- const toolUse = response.content.find(
- (block): block is Anthropic.ToolUseBlock => block.type === "tool_use",
- );
- if (!toolUse) return { entities: [], relations: [] };
-
- const input = toolUse.input as Partial<Extraction>;
- return {
- entities: (input.entities ?? []).filter((e) => e && e.name && e.type),
- relations: (input.relations ?? []).filter(
- (r) => r && r.source && r.target && r.relation,
- ),
- };
- }
-}
diff --git a/src/ai/local.ts b/src/ai/local.ts
index adc866ee..81d0d95f 100644
--- a/src/ai/local.ts
+++ b/src/ai/local.ts
@@ -145,7 +145,7 @@ export class OllamaExtractor implements Extractor {
} catch (err) {
throw new Error(
`Could not reach Ollama at ${this.baseUrl}. Start it (https://ollama.com) and pull a model ` +
- `(e.g. \`ollama pull ${this.model}\`), or set ANTHROPIC_API_KEY to use cloud extraction. ` +
+ `(e.g. \`ollama pull ${this.model}\`), or set OPENROUTER_API_KEY to use cloud extraction. ` +
`Underlying error: ${err instanceof Error ? err.message : String(err)}`,
);
}
diff --git a/src/ai/openrouter.ts b/src/ai/openrouter.ts
new file mode 100644
index 00000000..0320a663
--- /dev/null
+++ b/src/ai/openrouter.ts
@@ -0,0 +1,133 @@
+import OpenAI from "openai";
+import type { Extractor } from "./providers.js";
+import type { Extraction } from "../graph/types.js";
+
+/**
+ * Entity/relationship extractor backed by OpenRouter (https://openrouter.ai).
+ *
+ * OpenRouter exposes an OpenAI-compatible chat-completions API in front of many
+ * model providers, so we reuse the `openai` SDK with a custom `baseURL` and rely
+ * on OpenAI-style tool calling to get schema-valid structured output. Pick any
+ * tool-calling model via `CONTEXT_GRAPH_OPENROUTER_MODEL` (default gpt-4o-mini).
+ */
+const EXTRACTION_TOOL = {
+ type: "function" as const,
+ function: {
+ name: "record_graph",
+ description:
+ "Record the entities and relationships extracted from the text as a knowledge graph.",
+ parameters: {
+ type: "object",
+ properties: {
+ entities: {
+ type: "array",
+ description: "Distinct real-world entities or concepts mentioned in the text.",
+ items: {
+ type: "object",
+ properties: {
+ name: { type: "string", description: "Canonical, specific name of the entity." },
+ type: {
+ type: "string",
+ description:
+ "Coarse category, lowercase, e.g. concept, system, service, api, person, org, policy, tool, event, metric.",
+ },
+ summary: {
+ type: "string",
+ description:
+ "One or two sentences describing the entity, grounded in the text.",
+ },
+ aliases: {
+ type: "array",
+ items: { type: "string" },
+ description: "Alternate names/abbreviations used for this entity.",
+ },
+ },
+ required: ["name", "type", "summary"],
+ },
+ },
+ relations: {
+ type: "array",
+ description: "Directed relationships between the extracted entities.",
+ items: {
+ type: "object",
+ properties: {
+ source: { type: "string", description: "Name of the source entity." },
+ target: { type: "string", description: "Name of the target entity." },
+ relation: {
+ type: "string",
+ description:
+ "Predicate in snake_case, e.g. depends_on, part_of, authenticates_with, produces, owns, replaces.",
+ },
+ description: {
+ type: "string",
+ description: "Short explanation of the relationship.",
+ },
+ },
+ required: ["source", "target", "relation"],
+ },
+ },
+ },
+ required: ["entities", "relations"],
+ },
+ },
+};
+
+const SYSTEM_PROMPT = `You are a knowledge-graph extraction engine. Given a passage of text, identify the salient entities/concepts and the relationships between them.
+
+Rules:
+- Prefer specific, canonical entity names over pronouns or vague phrases.
+- Use coarse lowercase entity types, e.g. concept, system, service, api, person, org, policy, tool, event, metric.
+- Use snake_case relation predicates, e.g. depends_on, part_of, authenticates_with, produces, owns, replaces.
+- Only include relationships where BOTH endpoints are in your entities list.
+- Deduplicate: merge obvious surface-form variants into one entity with aliases.
+- Do not invent facts that are not supported by the text.
+Always respond by calling the record_graph tool.`;
+
+export class OpenRouterExtractor implements Extractor {
+ private client: OpenAI;
+ private model: string;
+
+ constructor(apiKey: string, model: string, baseUrl = "https://openrouter.ai/api/v1") {
+ this.client = new OpenAI({
+ apiKey,
+ baseURL: baseUrl,
+ // Optional attribution headers OpenRouter surfaces in dashboards; harmless if unused.
+ defaultHeaders: { "X-Title": "Context Graph Engine" },
+ });
+ this.model = model;
+ }
+
+ async extract(text: string, opts?: { hint?: string }): Promise<Extraction> {
+ const userContent = opts?.hint
+ ? `Context hint: ${opts.hint}\n\nText:\n${text}`
+ : `Text:\n${text}`;
+
+ const response = await this.client.chat.completions.create({
+ model: this.model,
+ temperature: 0,
+ tools: [EXTRACTION_TOOL],
+ tool_choice: { type: "function", function: { name: "record_graph" } },
+ messages: [
+ { role: "system", content: SYSTEM_PROMPT },
+ { role: "user", content: userContent },
+ ],
+ });
+
+ const call = response.choices[0]?.message?.tool_calls?.[0];
+ if (!call || call.type !== "function") return { entities: [], relations: [] };
+
+ let parsed: Partial<Extraction>;
+ try {
+ parsed = JSON.parse(call.function.arguments) as Partial<Extraction>;
+ } catch {
+ return { entities: [], relations: [] };
+ }
+
+ return {
+ entities: (parsed.entities ?? []).filter((e) => e && e.name && e.type),
+ relations: (parsed.relations ?? []).filter(
+ (r) => r && r.source && r.target && r.relation,
+ ),
+ };
+ }
+}
diff --git a/src/ai/providers.ts b/src/ai/providers.ts
index 5c57bd2b..1dc79e84 100644
--- a/src/ai/providers.ts
+++ b/src/ai/providers.ts
@@ -22,19 +22,24 @@ export interface EngineConfig {
/** Path to the SQLite graph file. Env: CONTEXT_GRAPH_DB. Default: ./.context-graph/graph.db */
dbPath?: string;
- /** Anthropic key for extraction. Env: ANTHROPIC_API_KEY. */
- anthropicApiKey?: string;
- /** Extraction model. Env: CONTEXT_GRAPH_EXTRACTION_MODEL. Default: claude-haiku-4-5-20251001 */
- extractionModel?: string;
+ /** OpenRouter key for extraction. Env: OPENROUTER_API_KEY. */
+ openrouterApiKey?: string;
+ /** Extraction model (an OpenRouter model id). Env: CONTEXT_GRAPH_OPENROUTER_MODEL. Default: openai/gpt-4o-mini */
+ openrouterModel?: string;
+ /** OpenRouter API base URL. Env: OPENROUTER_BASE_URL. Default: https://openrouter.ai/api/v1 */
+ openrouterBaseUrl?: string;
- /** OpenAI key for embeddings. Env: OPENAI_API_KEY. */
+ /**
+ * OpenAI key for embeddings (optional — OpenRouter has no embeddings endpoint,
+ * so embeddings run locally unless this is set). Env: OPENAI_API_KEY.
+ */
openaiApiKey?: string;
/** Embedding model. Env: CONTEXT_GRAPH_EMBEDDING_MODEL. Default: text-embedding-3-small */
embeddingModel?: string;
/**
* Force the fully-local providers (in-process embeddings + Ollama extraction)
- * even when cloud API keys are present. Env: CONTEXT_GRAPH_LOCAL=1.
+ * even when an OpenRouter/OpenAI key is present. Env: CONTEXT_GRAPH_LOCAL=1.
*/
forceLocal?: boolean;
/** Local embedding model (transformers.js). Env: CONTEXT_GRAPH_LOCAL_EMBEDDING_MODEL. Default: Xenova/all-MiniLM-L6-v2 */
@@ -58,17 +63,18 @@ export interface EngineConfig {
// --- advanced: bring your own components ---
/** Override the storage backend (defaults to a SQLite store at dbPath). */
store?: GraphStore;
- /** Override the embedder (defaults to OpenAI). */
+ /** Override the embedder (defaults to local, or OpenAI when a key is set). */
embedder?: Embedder;
- /** Override the extractor (defaults to Claude). */
+ /** Override the extractor (defaults to OpenRouter, or local Ollama with no key). */
extractor?: Extractor;
}
/** Fully-resolved configuration with all defaults applied. */
export interface ResolvedConfig {
dbPath: string;
- anthropicApiKey?: string;
- extractionModel: string;
+ openrouterApiKey?: string;
+ openrouterModel: string;
+ openrouterBaseUrl: string;
openaiApiKey?: string;
embeddingModel: string;
forceLocal: boolean;
@@ -85,7 +91,8 @@ export interface ResolvedConfig {
export const DEFAULTS = {
dbPath: "./.context-graph/graph.db",
- extractionModel: "claude-haiku-4-5-20251001",
+ openrouterModel: "openai/gpt-4o-mini",
+ openrouterBaseUrl: "https://openrouter.ai/api/v1",
embeddingModel: "text-embedding-3-small",
localEmbeddingModel: "Xenova/all-MiniLM-L6-v2",
ollamaModel: "llama3.2",
@@ -100,11 +107,13 @@ export function resolveConfig(config: EngineConfig = {}): ResolvedConfig {
const env = process.env;
return {
dbPath: config.dbPath ?? env.CONTEXT_GRAPH_DB ?? DEFAULTS.dbPath,
- anthropicApiKey: config.anthropicApiKey ?? env.ANTHROPIC_API_KEY,
- extractionModel:
- config.extractionModel ??
- env.CONTEXT_GRAPH_EXTRACTION_MODEL ??
- DEFAULTS.extractionModel,
+ openrouterApiKey: config.openrouterApiKey ?? env.OPENROUTER_API_KEY,
+ openrouterModel:
+ config.openrouterModel ??
+ env.CONTEXT_GRAPH_OPENROUTER_MODEL ??
+ DEFAULTS.openrouterModel,
+ openrouterBaseUrl:
+ config.openrouterBaseUrl ?? env.OPENROUTER_BASE_URL ?? DEFAULTS.openrouterBaseUrl,
openaiApiKey: config.openaiApiKey ?? env.OPENAI_API_KEY,
embeddingModel:
config.embeddingModel ??
diff --git a/src/engine.ts b/src/engine.ts
index 4deeca80..10406094 100644
--- a/src/engine.ts
+++ b/src/engine.ts
@@ -4,7 +4,7 @@ import type { GraphStore } from "./graph/store.js";
import { SqliteStore } from "./graph/sqlite-store.js";
import type { Embedder, EngineConfig, Extractor, ResolvedConfig } from "./ai/providers.js";
import { resolveConfig } from "./ai/providers.js";
-import { AnthropicExtractor } from "./ai/anthropic.js";
+import { OpenRouterExtractor } from "./ai/openrouter.js";
import { OpenAIEmbedder } from "./ai/openai.js";
import { LocalEmbedder, OllamaExtractor } from "./ai/local.js";
import { chunkText } from "./ingest/chunker.js";
@@ -93,11 +93,12 @@ export class ContextGraphEngine {
if (this.cfg.extractor) {
// Explicit override wins.
this._extractor = this.cfg.extractor;
- } else if (!this.cfg.forceLocal && this.cfg.anthropicApiKey) {
- // Use cloud extraction when a key is available.
- this._extractor = new AnthropicExtractor(
- this.cfg.anthropicApiKey,
- this.cfg.extractionModel,
+ } else if (!this.cfg.forceLocal && this.cfg.openrouterApiKey) {
+ // Use OpenRouter for extraction when a key is available.
+ this._extractor = new OpenRouterExtractor(
+ this.cfg.openrouterApiKey,
+ this.cfg.openrouterModel,
+ this.cfg.openrouterBaseUrl,
);
} else {
// Fall back to local extraction via a running Ollama instance.
diff --git a/src/index.ts b/src/index.ts
index ca475ef6..af846fb1 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -54,6 +54,6 @@ export type {
} from "./graph/types.js";
// Providers, for advanced/custom setups.
-export { AnthropicExtractor } from "./ai/anthropic.js";
+export { OpenRouterExtractor } from "./ai/openrouter.js";
export { OpenAIEmbedder } from "./ai/openai.js";
export { LocalEmbedder, OllamaExtractor } from "./ai/local.js";