Dev.to AI 🤖 Ai 👁 0 📖 13 min read

My RAG busted me for text roleplaying

I loaded several years of my chats into a RAG so my AI agent would know me and my friends. And it did get to know me. Also Clyde and Craig from South Park, Aphelios from League of Legends, and the first person I ever fel

I loaded several years of my chats into a RAG so my AI agent would know me and my friends. And it did get to know me. Also Clyde and Craig from South Park, Aphelios from League of Legends, and the first person I ever fell for on the internet (a roleplayer who played Kaito). Along the way it decided I'm a tech guy, not a cheapskate, and a health nut (all in masculine grammatical gender, of course. Russian has that, more below).

This post is about how personal memory for an LLM assistant works, and why the hardest part isn't vectors or reranking, it's the question "whose fact is this, anyway" 👀

TL;DR

Two Telegram bots and a Mini App share one memory on Postgres + pgvector, with hybrid search (FTS + dense retrieval + RRF + Haiku reranking).

Facts were extracted by an LLM from my Telegram and VK chat exports (VK is the big Russian social network, think old-school Facebook). Every fact stores its author, the source quote and the date. The extraction was also set up to keep sensitive facts out for privacy, so only general stuff got in (preferences, general info). The database is personal: it lives on my server, only I see the facts about my friends, and nothing gets published. The texts do go through model APIs during processing, so for friends the model was told from the start to pull only general facts, nothing about health, money or private life.

Attribution by grammatical gender broke. Attribution by quote author and grammatical person worked.

Automation basically can't tell text roleplay from real life. Only manual review helps there.

Cleaning up the facts about me (about 1,600 of them) cost less than two cents in money and over an hour in real time. The expensive part wasn't the LLM, it was my attention 😭

What this system is

I have ADHD, so external memory isn't a toy for me, it's literally a prosthetic. So I built myself AI agents: two Telegram bots and a Mini App.

Nexus is my personal assistant. Tasks, finances, budget, lists, receipt photos, voice messages and memory: it manages my whole life, down to the cats' litter boxes and notes about Slava KPSS merch (Slava KPSS is a Russian rapper). You write to it in free text, it figures out what to do with that and reminds you when needed. Basically the brain center of my whole infrastructure.

Arcana is the CRM for my esoteric practice. Clients, readings, rituals. The stuff I try not to make too public, but that makes up a big part of my life. Yes, that happens 🌒

Mini App is a web interface inside Telegram. Same stuff, but pretty and with animations, including a Memory section, people cards and fact review.

Stack: Python, aiogram 3, FastAPI, Postgres + pgvector, Voyage embeddings, Claude Haiku for routine stuff and Claude Sonnet for the hard stuff. Runs on one VPS in Docker Compose + my subdomain heylark.dev. Deploys only go out after green tests in CI.

There's one memory for everything. It's a facts table split by subject: me, people, cats, everything else. Every fact in memory has its text ("doesn't eat cilantro"), a type (likes/dislikes, important, or other) and a category (hobbies, health, work, etc.). If the fact is about someone, it has a link to that person. It also has a source, meaning the quote the fact was pulled from, its author, platform and date, and a review status: pending, confirmed or rejected.

Every person, me included, has their own entity card with name, aliases, birthday, and TG and VK accounts. One person can have several accounts, two different people can share a name.

How the bot searches memory

RAG (Retrieval-Augmented Generation) is when the model doesn't answer from memory, but from chunks that search pulled from a database.

I built RAG into my projects for two reasons: ADHD, and because I know how 💅 I have a whole infrastructure of my own projects (it doesn't end with Nexus-Arcana), I'm a compulsive automator of my own life, and I just have fun with new interesting cases at work.

Honestly, my first version was embarrassing: substring search (ILIKE '%word%'), sorted by creation date. Vector search only kicked in if fewer than three facts were found. Asked "what don't I eat", it found anything except the list of things I don't eat. Here's the pipeline now:

Question parsing. No LLM, just rules. We figure out the subject (me, a specific person, several people with the same name, or no idea who) and the question type (likes, dislikes, what's important). Names are resolved with grammatical cases and aliases in mind: «что любит Сашенька» ("what does Sashenka like") and «что подарить Саше» ("what to get Sasha") land on the same card. (Russian names change their endings by case, and Sasha/Sashenka are forms of the same name.)

Route selection. If the person and question type are known, we go straight to SQL. Birthday comes from the card, not from facts. Everything else goes to hybrid search.

Hybrid search. Two searches run in parallel. Postgres full-text search (FTS) with Russian morphology finds exact words, and pgvector vector search (Voyage embeddings) finds meaning. Example: for "what don't I eat" it pulls the list of foods even if the word "eat" isn't there.

Merging with RRF (Reciprocal Rank Fusion). RRF is an algorithm for merging results from two different sources. It re-ranks documents without having to bring their scores to one scale, and pushes up the chunks that both methods rated highly. In my case every fact gets the sum of 1 / (60 + rank) from both lists. No score normalization, only the order matters.

Reranking. Claude Haiku looks at the top candidates and keeps only the ones that actually answer the question.

Generation with checks. Haiku writes a short answer as a list and returns JSON where every line has the ids of the facts it relies on. The code checks that those ids exist and were actually in the results (grounding), and a line with no support gets dropped. If everything breaks, the bot just shows the list of all found facts.

Under the answer there's a source ("based on facts from TG/VK, year") and an "All facts" button. Every request writes a trace: the route, what FTS found, what the vectors found, what the reranker kept and what went into the answer. Debugging RAG without a trace is reading tea leaves, and I'm a witch, I know what I'm talking about 🔮

Quality is measured with offline evals: recall@5 on a set of questions with known correct facts, plus a grounding check. On a synthetic set of 9 questions, recall@5 was 72% with substring search, 86% after hybrid FTS + RRF, and 100% after adding question parsing. The set is tiny, so this is more a check that the pipeline doesn't lie than a win over a benchmark.

If the person and question type are known, no search is needed. Everything else goes through both searches, fusion and reranking.

Where the facts came from

Filling this memory by hand would take me until retirement. But I do have years of Telegram and VK chats where people tell you what they like, what they're afraid of and when their birthday is. And where I tell people more about myself than I probably should 🫠

The export pipeline starts with the chat export from TG Desktop and the VK API. The chats get cut into windows and run through Haiku with the task "pull out facts about people". Output is JSON: fact text, type, verbatim source quote and quote author. The task was phrased so that only general, non-sensitive facts about other people came out, plus literally all facts about me. Spoiler: that's exactly where I got burned later 😎

Each fact is then linked to a person by the chat partner's account, name and aliases. Everything lands in the database with status "Pending". Then basic human-in-the-loop: we review facts in the Mini App (accept, reject, move to another person, merge duplicates, change category).

The source quote and its author turned out to be the most valuable decision in the whole system. Without them, a fact is an LLM retelling you can't argue with. With them, any mistake can be rechecked and fixed with rules, without running the LLM again.

The rule for all bulk edits: dry-run, cost estimate, before/after sample into a local file, my review, then apply with a backup. No edit writes to the database until a human has seen examples (you'll see why below).

The main problem: whose fact is it, anyway

There are two people in a chat. The dialogue looks something like this: one writes «я вообще не пью после того нового года» ("I don't drink at all since that New Year's"), and the other answers «а ты же вроде любишь вино да?» ("but you like wine, right?").

From this the model has to figure out who doesn't drink and who likes wine. During extraction Haiku retells facts in the third person ("doesn't drink"), so the person gets lost. As a result, my card filled up with everything my chat partners said about themselves. One friend gifted me 70 of their facts 😎

Quick Russian grammar break, the rest of this section depends on it. Russian verbs carry a lot in their endings. The ending shows the person, so the pronoun is often dropped: «не пью» is "(I) don't drink", «не пьёшь» is "(you) don't drink", «не пьёт» is "(he/she) doesn't drink". In the past tense the verb shows gender instead: «устал» is "got tired" said about a man, «устала» is the same about a woman. Short adjectives do it too: «готов» / «готова», "ready". So in Russian, "I'm tired" also tells you the speaker's gender.

Attempt 1: filter by gender

The first idea was obvious: I'm a girl, so «устал» ("tired", masc.), «пришел» ("came", masc.), «готов» ("ready", masc.) aren't about me. It produced a mountain of false positives, for two reasons. First, I often write about myself in the masculine (the internet, text roleplay, online games, pick your favorite). Second, when retelling, Haiku slides into the masculine by default: my «я устала» ("I'm tired", fem.) turns into «устал от работы» ("tired of work", masc.).

Gender in the fact text is an artifact of two noise sources, not a sign of who the author is.

Attempt 2: quote author and grammatical person

We look not at the retelling but at the verbatim quote: who wrote it and in which person. Gender plays no part in the rules at all.

Who wrote it What's in the quote Whose fact
Me «я» ("I") or a 1st-person verb, including without the pronoun («не пью», "(I) don't drink") Mine
Me «ты» ("you") as the subject, no «я» Chat partner's
Chat partner «я», talks about themselves in 1st person, including without the pronoun, no «ты» Chat partner's
Chat partner «ты» about me Mine
Anyone «я», «когда ты...» ("when you...") where «я» is the subject Quote author's
Anyone both persons, unclear which one is the subject Goes to manual review
Anyone the fact actually comes from the chat partner's message, not from this quote Reject: extraction garbage

Russian helps a lot here, because the person is readable from the verb ending even without a pronoun. This kind of attribution has upsides: it's deterministic, cheap and easy to explain. We can point at any fact and see which rule fired.

It took six iterations, and on every one I read a sample. Result: 70 facts went back to the person they belonged to in the first place, and 50 more landed in "Check author". Along the way I also found a person card built from someone else's account (the account belonged to a completely different person).

The funny part: what the bot thinks of me

Once authorship was sorted out, my card still had about 1,600 facts. They had to be sorted into categories, cleaned of garbage and switched to the feminine. And that's where it started 🚬

Haiku rewrites the texts, and the code accepts an edit only if it changed the ending, not the meaning. Word count has to match, numbers too, and similarity to the original can't drop below 80%. Without these checks the model starts "improving" things. Here's what the filter rejected. The fact «не жмот, любит делать людям приятное» ("not a cheapskate, likes doing nice things for people") turned into «не жадина» ("not stingy"). I don't think I need to explain the difference in meaning between "cheapskate" and "stingy" in this context 😁

Another fact, «в день рождения идет ко врачу» ("goes to the doctor on her birthday"), turned into «пошла» ("went", fem.). Not critical, since the fact is from the past, but the gender and the tense changed together.

And «бесит объяснять, с кем общается» ("hates explaining who she hangs out with") turned into «с кем общаешься» ("who you hang out with"). That's not gender anymore, that's person, and the filter let it through. I caught it while proofreading by hand.

Pronouns are a separate story. So gender replacements go as a separate list, and I check each one by hand.

Roleplay: a biography I never had

And this is where I got busted 🚨 Facts like "has a son", "helps people separately from her sister", "spent ages looking for a decent roleplay partner for Craig/Clyde" (a South Park ship) honestly made it from the chats into memory.

I don't have a son. Or a sister. As for helping people, the most I do is donate to animal shelters and do my esoteric practice. But from the pipeline's point of view everything is correct: the quote author is me, there's a first person, so the fact is mine. The attribution rules worked flawlessly, it's just that in text roleplay "I" isn't real-life me. And rules can't fix that, because nothing in a chat tells the model that for the last two hours people were writing as characters. That's why I built in manual review with a "Reject" button.

Categories: keywords take everything literally

Categorization has two stages. Keywords go first, they're free and deterministic. That sorted 2/3, the rest went to Haiku. Keywords take everything literally. The fact "working on learning to love herself" went into "Work". Can't argue with that 🙃 "Dreams of getting a ferret" went into "Cats". A ferret isn't a cat, but the spirit is right 🐾 And "26 years old" went into "Household". I mean, age is kind of a household problem. The fact is outdated though (EXPIIIIRED)

This gets fixed with targeted edits by id, no LLM, and in the Mini App with one tap. I wasn't aiming for perfect automation, I needed automation that's cheap to correct.

Duplicates: the same fact dozens of times

I tell different people the same things about myself, and every retelling becomes a separate fact. Duplicate search uses cosine similarity of embeddings with a 0.90 threshold: the system finds facts close in meaning even if they're worded differently. That gave 155 groups of duplicate candidates. But nothing gets merged automatically, because "likes coffee" and "doesn't like coffee" are almost the same thing to an embedding. For that I made "Merge", "These are different" and undo-merge buttons in the Mini App.

Numbers and money

Cleaning up the "Me" section cost less than two cents. My time reading samples: priceless.

Pass Volume Cost, USD
Categorization (keywords + Haiku) 1402 facts, 405 of them via LLM 0.0033
Leftover categories 75 facts 0.0003
Switching to feminine 1550 checked, 63 rewritten 0.0122
Re-embedding with Voyage 63 facts 0.00002

The live bot is cheap too: about $0.7 a week for everything. The classifier has the same 14-16k token prefix on every message, and it gets cached. Reading from cache costs 10% of regular input. But cheap calls don't guarantee your balance won't run out 😭 Mine ran out twice, because several projects were on one key. So here's a lil tip, bro to bro: a separate key for scripts, a separate workspace with a limit per project (in the Anthropic Console limits are set per workspace, not per key), and a Telegram alert when the API returns a billing error.

What I took away from this

RAG over personal data is mostly data cleaning. I built hybrid search with reranking in a couple of evenings, but attribution, categories and duplicates took way longer.

Store the verbatim quote and its author. An LLM retelling loses person, gender and context, so the quote lets you recompute anything (cheaply and without an LLM!)

Don't trust gender where you have person. And there's no point building rules on a person's gender at all, because people write about themselves however they want.

All bulk edits go through dry-run, sampling and backup. Plus a strict validator on the output, because if you let the model fix gender, it'll change the style, the tense and the person too.

You can't pull context out of a single message. Roleplay, a joke, sarcasm, a movie quote all look the same to the model, so everything imported sits as "Pending" until a living human looks at it.

Manual review isn't automation failing, it's part of the design. Automation's job is to make review fast, the human's job is to confirm the review is right.

And now I have a bot that is fully convinced my dad owns a shoe business in Colorado and that I played Aphelios. The second one, unlike the first, is true.

Originally published at heylark.dev. AI-translated from Russian, edited by me.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.