Conversations exceeding context limits degrade reasoning. Implement a dual-tier memory system:
1. Working Buffer (Hot Context) Retain the last 6-10 conversational messages in raw fidelity for immediate conversational cohesion.
2. Periodic Compaction (Warm Memory) Every 10 messages, trigger an asynchronous background LLM call to condense historical dialogue into an evolving markdown scratchpad:
markdown
## Key Decisions & Facts Established - User preferred database: Neon PostgreSQL with Prisma ORM - Production deployment target: Vercel serverless edge - Active auth provider: NextAuth v5
Inject this 200-token summary as a persistent system prompt block, saving thousands of tokens per session.