Skip to content

OpenAI SDK

bash
npm install openai memvault

Setup

typescript
import OpenAI from "openai"
import { MemVault } from "memvault"
import { createMemVaultTools } from "memvault/openai"

const openai = new OpenAI()
const vault = new MemVault({ db: prisma.memvaultMemory })
const { tools, handleToolCall } = createMemVaultTools({ vault, tenantId: user.id })

Agentic loop

typescript
const messages = [
  {
    role: "system",
    content: "Always call memvault_recall before responding. Save preferences with memvault_remember.",
  },
]

async function chat(userMessage: string) {
  messages.push({ role: "user", content: userMessage })

  while (true) {
    const response = await openai.chat.completions.create({
      model: "gpt-5.4",
      tools,
      messages,
    })

    const msg = response.choices[0].message
    messages.push(msg)

    if (!msg.tool_calls?.length) break

    for (const tc of msg.tool_calls) {
      const input = JSON.parse(tc.function.arguments)
      const result = await handleToolCall(tc.function.name, input)
      messages.push({ role: "tool", tool_call_id: tc.id, content: result })
    }
  }
}

TIP

GPT-4o benefits most from the system prompt instruction. Without it, memvault_recall may not be called at session start.

Released under the MIT License.