Skip to main content
Back to integrations
Vercel
FrameworkMemory tools

LLMSlim + Vercel AI SDK

Long-term memory for AI SDK agents as tools, with no LLM writing your memory and nothing injected into the prompt.

The llmslim npm package is a client for an llmslim serve server. Its llmslim/ai-sdk entry gives the model recall_memory and remember tools, and a middleware that saves each user message and reply. Messages are stored word for word and selected at read time within a token budget, so nothing is summarized or extracted into a wrong fact. Recalled memory reaches the model as a tool result, which your app keeps in its history like any other message.

1. Installation and setup

The client talks to an llmslim serve server (Python, one process, one SQLite file or Postgres). Call it from your backend: an API key gives access to every user's memory.

terminalbash
npm install llmslim ai   # ai 7; the client alone has no dependencies

Start the memory server:

terminalbash
pip install "llmslim[server]"
llmslim serve          # http://127.0.0.1:8080; set LLMSLIM_API_KEYS to serve beyond localhost

2. Architecture & execution flow

1

memoryMiddleware wraps your model. After each call it saves the newest user message and the model's text reply to llmslim serve; in a tool loop the user message is saved once. It never changes the prompt.

2

memoryTools gives the model recall_memory and remember. The user, agent and run ids come from your code, never from a tool argument, so a model steered by injected text can't read another user's memory.

3

recall_memory returns the stored messages that answer the query, whole and under their dates, within a token budget, labelled as data, not instructions.

4

Because memory arrives as a tool result, the history your app resends is exactly what the model saw before: prompt caches keep hitting and Claude's preserved thinking stays valid.

3. Production code pattern

agent.tstypescript
import { generateText, stepCountIs, wrapLanguageModel } from "ai";
import { anthropic } from "@ai-sdk/anthropic";
import { LLMSlim } from "llmslim";
import { memoryMiddleware, memoryTools } from "llmslim/ai-sdk";

const client = new LLMSlim(); // LLMSLIM_BASE_URL and LLMSLIM_API_KEY, or pass baseUrl and apiKey
const model = wrapLanguageModel({
  model: anthropic("claude-haiku-5-5"),
  middleware: memoryMiddleware({ client, userId: session.userId }), // saves each user message and reply
});

const result = await generateText({
  model,
  tools: memoryTools({ client, userId: session.userId }), // recall_memory and remember
  stopWhen: stepCountIs(5),
  messages,
});

4. Options

Keyword arguments, shown as name = default.

client = required
An LLMSlim client: new LLMSlim({ baseUrl, apiKey }), defaults from LLMSLIM_BASE_URL and LLMSLIM_API_KEY.
userId / agentId / runId = one required
Whose memory the tools and middleware read and write.
budget = the server's
Most tokens recall_memory returns (memoryTools only).
onError = console.warn
Called when saving fails; the model's answer is returned either way (memoryMiddleware only).

5. Deployment best practices

Run llmslim serve next to your app (Docker image or pip), with an API key and Postgres when several instances share one memory. Create the client on your server, map your signed-in user to userId, and pass memoryTools and memoryMiddleware to generateText or streamText.

6. Key optimization tips

  • Let the model call recall_memory before answering anything that may depend on earlier conversations; the tool description says so.
  • Keep the user's own words: remember stores text verbatim, and recall matches on words.
  • Use runId to keep one conversation's memory separate when that matters.

7. Limitations

  • Lexical recall matches words, not meanings: forms of a word match (move, moved), but a paraphrase such as allergy against allergic can miss. The server's semantic mode (llmslim[semantic]) covers paraphrases.
  • The client needs fetch: Node 18 or newer, Bun, Deno or an edge runtime. The ai-sdk entry needs ai 7 (Node 22).

Frequently asked questions

Troubleshooting

Further reading