GeniffyDocs
Changelog Log In Get a key

Vapi

Give a Vapi phone assistant a memory of each caller. Vapi calls your server at three moments: when a call starts, when the assistant uses a tool, and when the call ends. Answer the first with what matters about the caller, the second with a lookup, and save the call at the third. The next call starts knowing what was said in this one.

Install

Terminal
pip install fastapi uvicorn geniffy

Set GENIFFY_API_KEY from API keys in the Geniffy app, and VAPI_SECRET to a secret you also give Vapi.

Your server

server.py
import asyncio
import hmac
import json
import logging
import os

from fastapi import BackgroundTasks, FastAPI, Header, HTTPException, Request
from geniffy import AsyncGeniffy

geniffy = AsyncGeniffy()                          # reads GENIFFY_API_KEY
app = FastAPI()
log = logging.getLogger("vapi")
ASSISTANT_ID = os.environ.get("VAPI_ASSISTANT_ID", "your-assistant-id")
OPENING = "A call is starting. What matters about this caller?"
NOTHING = "Nothing is known about this caller yet. If they mention an earlier call, say so rather than guessing."
UNREAD = "This caller's history could not be read just now. If they mention an earlier call, ask rather than guess."
# what Twilio sends in place of a withheld number: ANONYMOUS, RESTRICTED, BLOCKED, UNKNOWN, UNAVAILABLE
WITHHELD = {"266696687", "7378742833", "2562533", "8656696", "86282452253"}


def space_for(number: str | None) -> str | None:
    """The caller, by the digits of their number. None when it is withheld or missing: then nothing is read or
    saved, rather than one memory shared by every unknown caller. If you know callers as customers, use your
    customer id, and the same memory serves their calls, chats and emails."""
    digits = "".join(ch for ch in number or "" if ch.isdigit())
    if not 7 <= len(digits) <= 15 or digits in WITHHELD:
        return None
    return f"user_{digits}"


async def known_about(space: str | None, cue: str, budget: int) -> str:
    """The caller's briefing, for the prompt. A call never waits on it for more than two seconds."""
    if space is None:
        return NOTHING
    try:
        return await asyncio.wait_for(geniffy.space(space).briefing(cue=cue, budget_chars=budget), 2) or NOTHING
    except Exception as e:                        # slow or failing: the call goes on without it
        log.warning("no briefing for this call: %r", e)
        return UNREAD


async def save_call(space: str, call_id: str, turns: list[dict]) -> None:
    """The call as one conversation under its own id, so a call sent again is the same source. A call of more
    than 500 turns, the most one add takes, goes in parts."""
    for at in range(0, len(turns), 500):
        await geniffy.space(space).memories.add(messages=turns[at:at + 500], title="Call",
                                                external_id=call_id if at == 0 else f"{call_id}-{at // 500}")


@app.post("/vapi")
async def vapi(request: Request, background: BackgroundTasks, x_vapi_secret: str = Header("")):
    if not hmac.compare_digest(x_vapi_secret, os.environ["VAPI_SECRET"]):
        raise HTTPException(401)
    message = (await request.json())["message"]
    call = message.get("call") or {}
    space = space_for((call.get("customer") or {}).get("number"))

    if message["type"] == "assistant-request":    # a call is starting: what to know before talking to them
        memory = await known_about(space, OPENING, 2500)
        return {"assistantId": ASSISTANT_ID, "assistantOverrides": {"variableValues": {"memory": memory}}}

    if message["type"] == "tool-calls":           # the assistant looks something up mid-call
        results = []
        for tool_call in message.get("toolCallList") or []:
            fn = tool_call.get("function") or {}
            args = fn.get("arguments") or tool_call.get("arguments") or tool_call.get("parameters") or {}
            args = json.loads(args) if isinstance(args, str) else args
            found = await known_about(space, args.get("query") or OPENING, 1500)
            results.append({"name": fn.get("name") or tool_call.get("name"), "toolCallId": tool_call["id"],
                            "result": " ".join(found.split())})      # Vapi reads a result as one line
        return {"results": results}

    if message["type"] == "end-of-call-report" and space:   # the call is over: save who said what
        turns = [{"role": "assistant" if m["role"] in ("assistant", "bot") else "user", "content": m["message"]}
                 for m in (message.get("artifact") or {}).get("messages") or []
                 if m.get("role") in ("user", "assistant", "bot") and m.get("message")]
        if turns:                                 # saved after Vapi has its answer, so it never waits on the save
            background.add_task(save_call, space, f"vapi-{call['id']}", turns)
    return {}

Run it with uvicorn server:app, somewhere Vapi can reach over HTTPS.

Both moments read the caller's briefing: where things stand with them, what is due or was promised, what happened on earlier calls, then the facts, each dated. It is read, not written by a model, so it comes back in a fraction of a second, well inside a ringing phone or a pause mid-sentence; past two seconds, the call goes on without it. When something changed between calls (a payment moved from Friday to Monday), the briefing carries the new date, and the old one stays in the memory as history.

A number says which customer is probably calling, not who is: caller ID can be faked. A withheld or missing number gets no memory, and its call is not saved. Before your assistant shares anything private from what it remembers, have it confirm who it is speaking with; the production checklist says how.

In Vapi

In your assistant's system prompt, put the memory where you want it:

You are a friendly phone assistant. Keep answers short.

What is known about the caller, found by their number. Before you share anything private from it, confirm who
you are speaking with.
<memory>
{{memory}}
</memory>
  1. Add a function tool named recall, with one string parameter, query, and the description Look up what is known about the caller, each item dated. Point its server URL at https://your-server/vapi.
  2. On your phone number, set the server URL to https://your-server/vapi, so each call asks it for the assistant, and turn on the end-of-call report.
  3. Make a credential for the header X-Vapi-Secret with your VAPI_SECRET, and use it on both server URLs.

When nothing is known about a caller, the memory says so in one sentence, and so does recall, so the assistant says it doesn't know instead of guessing. Each call is added as one conversation titled Call, under the call's own id, so a report sent twice is saved once. Geniffy keeps who said what, so what the caller said becomes a fact about them, and what your assistant said stays the assistant's.

Last updated October 16, 2026