Integrations

Vercel AI SDK tools

Give a Vercel AI SDK model memory with ready-made tools and prompt context

@neurophic/ai-sdk wraps the JavaScript SDK for the Vercel AI SDK.

Install

npm install neurophic @neurophic/ai-sdk ai zod

Needs ai 7 and Node 22 or later.

createTools

import { Neurophic } from 'neurophic';
import { createTools } from '@neurophic/ai-sdk';

const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const { remember, recall } = createTools({
  client,
  identifier: 'user-123',
  name: 'Ada',
  search: { roles: ['user', 'assistant'], sources: true, rerank: 'fast' },
});

createTools returns two AI SDK tools you can pass straight to streamText or generateText:

search is applied to every recall call and is set by you, never by the model: roles for whose words to return, sources to also return the original turns, and rerank for a second ranking stage before the cut.

Each tool carries a description and a zod input schema, so the model knows when and how to call it; invalid input is rejected before any request is made.

Example

import { Neurophic } from 'neurophic';
import { createTools } from '@neurophic/ai-sdk';
import { streamText, isStepCount } from 'ai';
import { openai } from '@ai-sdk/openai';

const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const { remember, recall } = createTools({ client, identifier: 'user-123' });

const instructions = 'You are a travel assistant. Use recall before answering and remember what the user shares.';

const result = streamText({
  model: openai('gpt-4o'),
  instructions,
  tools: { remember, recall },
  stopWhen: isStepCount(3),
  messages: [
    { role: 'user', content: 'Remember that I prefer window seats on long flights.' },
  ],
});

Learn more under API reference > Ingest and API reference > Retrieve.

Warning:If a call fails, the tool's execute throws a NeurophicError and the AI SDK reports it as a tool error for that step.

withContext

withContext calls client.context and returns your instructions with the context appended, as one string. Use it when you want memory in front of the model on every turn without giving it a tool.

import { Neurophic } from 'neurophic';
import { withContext } from '@neurophic/ai-sdk';
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

const client = new Neurophic({ apiKey: process.env.NEUROPHIC_API_KEY });
const userMessage = 'Can you find me a flight to Lisbon next month?';

const instructions = await withContext(client, {
  identifier: 'user-123',
  query: userMessage,
  instructions: 'You are a travel assistant.',
});

const result = streamText({
  model: openai('gpt-4o'),
  instructions,
  messages: [{ role: 'user', content: userMessage }],
});

withContext takes:

  • query. The text to fetch context for, usually the latest user message.
  • instructions. Optional; your own instructions for the model.
  • roles, sources, rerank. Optional; the same search options as createTools, see API reference > Context.
  • template. Optional (context, instructions) => string; the default appends a ## Context from memory heading and the context lines to instructions, or returns just that block when instructions is not set.

With the default template and the example above, the returned string is:

You are a travel assistant.

## Context from memory
- Prefers window seats on long flights. (memory)
Warning:withContext returns instructions unchanged (or an empty string when instructions is not set) in two cases: when nothing relevant is found, and when the request fails for any reason. Errors are not thrown.

Learn more under API reference > Context.