Use Brain with OpenRouter

OpenRouter sends one request format to hundreds of models. It has no place to plug in a memory service: its chat page has no connectors, and its API runs only its own fixed set of server-side tools, with no way to attach a remote MCP server. So for an app on OpenRouter, memory has to live on your side of the call.

Brain ships a small module for exactly that: integrations/openrouter/brain-openai.mjs. It works with OpenRouter and any other OpenAI-compatible Chat Completions endpoint (OpenAI, Groq, Together, a local server), and it reads and writes the same ~/.brain your coding agents use.

What it does

  • Recall on every turn. withBrainContext() runs brain recall on the new user message and puts the matches in a system message just before it. The block is capped at 600 tokens by default, weakest matches dropped first.
  • One turn only. The recall block goes into this request, not into your history. The next turn gets a fresh recall instead of a growing pile.
  • Tools the model can call. BRAIN_TOOLS defines brain_recall and brain_memorize as OpenAI function tools; runBrainTool() runs a tool call and returns the tool message.
  • Honest provenance. brain_memorize takes an origin. Facts taken from tool output or web pages are held for your review (brain verify) instead of becoming established memory.
  • Fails soft. If the brain CLI is missing or errors, the request goes out without memory.

Recall works with every model. The tools need a model with tool calling.

Set up

You need Node 18 or later and the brain CLI with a brain to read:

npm i -g brain-memory
brain --claude    # or any installer flag; it creates ~/.brain

Copy brain-openai.mjs from the repository into your app. It has no dependencies.

import { BRAIN_TOOLS, OPENROUTER_URL, openRouterHeaders, runBrainTool, withBrainContext } from "./brain-openai.mjs";
 
let history = [];
 
async function chat(userText, conversationId) {
  // 1. Recall for this message; `messages` carries the block, `history` does not.
  let { messages, history: next } = withBrainContext(history, userText, { session: conversationId });
 
  for (let step = 0; step < 4; step++) {
    const res = await fetch(OPENROUTER_URL, {
      method: "POST",
      headers: openRouterHeaders(process.env.OPENROUTER_API_KEY),
      body: JSON.stringify({ model: "anthropic/claude-sonnet-5.5", messages, tools: BRAIN_TOOLS }),
    });
    const msg = (await res.json()).choices[0].message;
    messages = [...messages, msg];
    next = [...next, msg];
    if (!msg.tool_calls?.length) break;
 
    // 2. Run the brain tools the model asked for.
    for (const call of msg.tool_calls) {
      const out = runBrainTool(call, { session: conversationId });
      if (out) { messages.push(out); next.push(out); }
    }
  }
  history = next;
  return messages.at(-1).content;
}

For another OpenAI-compatible endpoint, send the same body to its URL with its own key.

tip

Try it from a clone of the repository: OPENROUTER_API_KEY=… node integrations/openrouter/example.mjs "what did we decide about X?". Set BRAIN_OPENROUTER_MODEL to try another model.

Memories written this way land in ~/.brain/captured/, tagged as coming from openrouter, and Claude Code, Codex and every other connected agent can recall them. Run brain cloud sync to merge them with Brain Cloud, so the hosted connector has them too.

Using OpenRouter's agent SDK instead

OpenRouter's own TypeScript SDK, @openrouter/agent, can connect to a remote MCP server on the client side and run the tool loop for you. Point its createMCPTools() at the hosted connector, https://mcp.brainmemory.ai/mcp, with its OAuth option, and the model gets every Brain tool (recall, memorize, pin, status and the rest) without the local CLI. See OpenRouter's MCP tools guide for the call shape.

not yet tested

We have not tested the @openrouter/agent route ourselves yet. The hosted connector signs in with OAuth only, so it needs a browser for the first sign-in and suits interactive apps better than servers.