DEV Community

Cover image for Teaching an AI assistant to forget a deleted Teams message
Akshat Gupta
Akshat Gupta

Posted on Edited on

Teaching an AI assistant to forget a deleted Teams message

Friday, late afternoon. Someone posts a price in a Teams channel. It is wrong. They notice, and they delete the message.

Problem solved? Not quite.

On Monday your AI assistant is still quoting that price. It does not hedge. It does not say "I think". It states the number like a fact.

The message is gone from Teams. It lives on in the assistant's head.

I wanted to fix that. So for Mergetober I built a Microsoft Teams connector for Cognee. The code lives in PR #437 for issue #4542.

What it does

Cognee turns documents into a knowledge graph that an agent can search. The connector feeds it from Teams channels. You pick the teams, and optionally the channels. Then:

  • Every thread becomes one document. The root post comes first, then the replies in time order.
  • An edit, or a deleted reply, re-writes the whole thread, so a conversation stays in one piece.
  • Later runs read only what changed.
  • Deleted threads are forgotten.
  • Only standard channels are read. No 1:1 chats, no group chats, no private channels.

Pipeline: Teams, dlt, Cognee

source = teams_source(team_ids=["<team id>"])
await cognee.remember(source, dataset_name="teams",
                      write_disposition="merge")
Enter fullscreen mode Exit fullscreen mode

The app credentials come from MS_GRAPH_TENANT_ID, MS_GRAPH_CLIENT_ID and MS_GRAPH_CLIENT_SECRET. The Graph client is a separate small helper, so the SharePoint and Outlook connectors can reuse it.

Getting in is the hard part

Teams has no API key you can paste. Here is the real recipe:

  1. Register an app in Microsoft Entra.
  2. Give it a client secret.
  3. Add two application permissions: ChannelMessage.Read.All and Channel.ReadBasic.All.
  4. Ask a tenant admin to click "Grant admin consent".

Skip step 4 and the app cannot read a single message. A normal user cannot do it for you. That click is the real wall in this issue. The README walks through every step, and covers the option where a team owner grants access to just their own team.

I had no tenant, so I started a Microsoft 365 Business Basic trial. It is free for a month but asks for a card. Set a reminder to cancel.

How to find what changed

I wanted Graph's delta query for channel messages. As far as I could tell from Microsoft's docs, delta only exists for chats, not for channels.

But the plain list of channel messages has a useful rule. Microsoft says results are sorted by the last modified date of the whole reply chain, newest first. A new reply pushes its thread to the top.

That is enough. Walk down from the top and stop at the first thread older than the cursor, minus five minutes for safety. Everything below is never touched.

How the cursor walks a channel

With nothing changed, the fake-API tests show one page request per channel.

Catching deletes

Graph does not drop a deleted message. It keeps it in the list, with deletedDateTime set and an empty body. The connector turns that into a hard delete. dlt removes the row, and Cognee's orphan cleanup removes it from the graph and the vector store.

Safety rules, because a wrong delete is worse than a missed one:

  • A failed request, a 403 or a rate limit never deletes anything. The run stops and the next one starts from the same spot.
  • A channel list that suddenly comes back empty forgets nothing.
  • A deleted or deselected channel forgets the threads it gave, and only those.

The real test

I tested against a fake Graph first. It plugs in underneath a real HTTP session, so dlt's real sign-in, paging and retry code runs. 48 tests pass, including one that runs the whole Cognee pipeline with a mocked LLM and proves an unchanged re-sync makes no LLM call.

Then I made a real team, posted real messages, and ran the example.

Three syncs against a real team

A deleted post whose replies live on

This one surprised me.

I posted a thread about a new freight partner in Mumbai, with one reply. Then I deleted only the root post. Teams keeps showing the reply. Graph still returned it too.

Delete the post, keep the reply?

I thought about keeping the orphaned reply. I decided against it. A reply without its post says little by itself, and a connector built to forget should lean towards forgetting. The README says so plainly.

The junk only a real LLM could show me

My first version labelled replies like this: Reply from Akshat Gupta (2026-10-20 05:45 UTC):.

Nothing in the fake tests could complain. A real model did. The graph grew entities called reply from akshat gupta at 2026-10-20 05:45 utc 1. A timestamp, promoted to a character in the story.

So I turned the labels into sentences: Akshat Gupta replied on 2026-10-20 at 05:45 UTC:. I ran the same two threads into two fresh datasets. The timestamp entities were gone both times. Some noise stayed, for example first reply in post a in one run, and the model is not deterministic. Two samples is all I have. It is better, not fixed.

What I did not test

Only a small team went through the live run, with one channel. These only have fake-API tests: paging past 50 threads, long reply chains, a real 429 from Graph, and keeping private and shared channels out. I did not try the team-owner consent route. I only ran Python 3.12.

One more limit. If a retention policy erases a message completely instead of marking it deleted, Graph has nothing left to report. That thread stays in memory until its channel is removed. The README says this too.

Try it

The example is in packages/connector/teams/examples/example.py. You need an Entra app with admin consent, and a team id. The team id is the group's Object ID in Entra.

Point it at your own team and tell me what breaks.

Top comments (0)