AI Agents
An AI agent is a system that uses a model to choose actions in an environment. Agents often combine reasoning, memory, tools, retrieval, and feedback so they can complete multi-step tasks.
How do you maintain data consistency during shared-memory migrations in multi-agent systems?
Maintaining data consistency during a shared memory migration means ensuring that agents see authoritative, correctly versioned information even while old and new storage formats coexist. The main risk is not just a failed conversion: agents may continue writing to the old format, overwrite a newer update, or retrieve stale indexed data. Beginner friendly example An API testing platform has requirements, generation, and review agents that share a durable memory record for endpoint authentication. Old memory: auth="OAuth2". New memory: auth={"scheme":"OAuth2","scopes":["pay:write"]}. While 5,000 records are being migrated, the requirements agent may update one endpoint's scopes. A migration worker must not overwrite that new information using the old snapshot. Choose an authoritative write path → Capture revisions → Backfill conditionally → Reconcile ongoing updates → Verify reads and indexes → Switch consumers Numerical example: a race condition Consider record m42: 1. At 10:00 , a migrator reads revision 7 , where the requirement contains only OAuth2. 2. At 10:01 , the requirements agent updates the record to revision 8 , adding a verified pay:write scope. 3. At 10:02 , the migrator attempts to write a converted copy based on revision 7. 4. An unsafe unconditional write could overwrite revision 8 and lose the new scope. 5. With compare and swap , the migration updates only if the current revision is still 7. Because it is now 8, the update is rejected; the migrator rereads and transforms revision 8 instead. In a database, this can be implemented with a conditional write such as UPDATE ... WHERE id = ... AND revision = 7, checking that exactly one row changed. A zero row result means another writer changed the record or it was removed. Techniques for consistent migration 1. One authoritative source: Define where accepted writes live throughout the transition. Avoid treating two independently writable copies as equally authoritative. 2. Revisioned writes: Use optimistic concurrency checks, transactions, or carefully scoped locks to protect updates from being overwritten. 3. Snapshot plus change capture: Backfill a consistent snapshot, then apply changes made after that snapshot through a durable change log or controlled event stream. 4. Controlled dual writes: If old and new stores both need updates, coordinate them using a transaction when supported or a transactional outbox plus retries and reconciliation. Two unrelated writes cannot be assumed atomic. 5. Idempotent migration: Give each converted record a stable ID, source revision, and migration version so retries do not duplicate or regress data. 6. Version aware reads: During transition, read the new representation only when it has a validated revision; otherwise use a known good authoritative source. 7. Index and cache reconciliation: Refresh vector indexes and invalidate stale caches after committed updates; track which source revision each index entry represents. 8. Repair and rollback plans: Preserve original versions, record mismatches, and stop promotion on unexplained divergence. 9. Tenant isolation: Retain ownership and access control metadata through every transform, including embeddings and caches. Reconciliation example Suppose 10,000 records must be migrated and 9,900 match the authoritative source after backfill. The apparent match rate is 9,900 / 10,000 = 99% , leaving 100 mismatches . The correct next action is not to declare success based on 99%. Classify the 100 discrepancies: newer writes, invalid source data, transform errors, or missing index updates. Set acceptance thresholds based on impact; zero mismatched authorization records may be required before switching security sensitive consumers. What should consistency checks compare? Stable record ID, tenant, and source provenance. Logical content, including required scopes—not just serialized JSON equality. Current revision and last applied source event. Required fields and explicit unknown values. Corresponding cache and search index revision. For semantically equivalent old and new shapes, use a canonical comparison rather than comparing raw strings. LangGraph and Celery considerations In LangGraph , a checkpoint may reference a shared memory revision. On resumption, check whether the referenced record is still valid or whether the workflow must reload authoritative data. Checkpoint compatibility alone does not guarantee current memory consistency. With Celery , backfill and change application tasks may be retried, redelivered, or executed out of order. Carry record revisions and stable event IDs, apply changes conditionally, and avoid allowing an older retry to undo a newer update. Difference from related lessons Shared state consistency covers normal concurrent agent updates. Shared memory schema migration covers changing a durable record's format. Consistency during migration covers keeping old/new data, live writes, caches, and indexes synchronized while that format change is underway. In short Use a single authority, revision checked writes, durable change capture, idempotent backfills, and semantic reconciliation. Cut over only after validating that agents retrieve correct, current, tenant isolated information.
How do you validate a shared-memory migration before switching all agents to the new schema?
Validate a shared memory migration before cutover by proving that the new schema preserves meaning, access controls, freshness, and retrieval behavior—not merely that the records can be parsed. The final decision should be a measurable go/no go gate backed by independent checks. Beginner friendly example: API test generation agents Three agents share durable endpoint knowledge: a requirements extractor writes it, a test generator reads it, and a security reviewer checks the resulting test cases. The old record stores auth: "OAuth2"; the new record stores auth: {"scheme": "OAuth2", "scopes": ["pay:write"]} plus source provenance. A migrated record is valid only if the scopes came from an authoritative requirement, the version is current, and the right tenant and agents can access it. Do not invent missing scopes or treat unknown information as approval. Define acceptance gates → Validate converted records → Compare old/new reads → Test live updates → Exercise agent consumers → Canary → Cut over or stop Numerical cutover example Suppose the migration covers 10,000 records : 1. 9,990 records pass structural checks; 10 fail. 2. Of the 9,990 structurally valid records, 30 contain an incorrect source revision or missing authorization evidence. 3. Therefore 9,960 / 10,000 = 99.6% pass both structural and semantic validation. 4. In a 500 query retrieval comparison, 490 return the expected authorized evidence under the new store, an observed 98% pass rate. 5. The release criteria require 100% correct authorization and tenant isolation , at least 99.9% verified record correctness , and at least 99% retrieval check success. Result: NO GO. The record correctness rate is below 99.9%, the retrieval rate is below 99%, and the security sensitive cases require zero known violations. Thresholds are illustrative; production criteria must match business and safety requirements. Do not hide failures with an overall average. What validation should be performed? 1. Completeness: Reconcile expected and migrated record IDs, counts, and missing/deleted records by tenant and source. Account for writes made after the initial snapshot. 2. Structural correctness: Check schema versions, types, required fields, constraints, and references; quarantine unsupported or corrupt records. 3. Semantic equivalence: Compare canonical business meaning. For example, OAuth2 without known scopes must not silently become OAuth2 with an invented scope. 4. Provenance and freshness: Verify source document version, record revision, timestamps, and last applied change event so stale converted data cannot override new updates. 5. Access control: Test cross tenant isolation, least privilege agent reads, and sensitive data handling against both positive and negative authorization cases. 6. Retrieval quality: Test representative agent questions and expected evidence, including keyword and vector search behavior, metadata filters, missing records, and cross tenant negatives. 7. Concurrent updates: Inject a write while migration/backfill runs; ensure revision checks or a change log catch up preserve the newest authoritative value. 8. Integration behavior: Run requirements, test generation, and security review agents with the migrated memory; confirm validated output and safe handling of incomplete context. 9. Operational performance: Measure p95 retrieval latency, errors, index lag, migration backlog, and resource utilization against release criteria. 10. Recovery readiness: Demonstrate backups, rollback/routing procedures, and an owner approved response to failed gates. How do you compare old and new memory safely? Use shadow reads : serve production requests from the authoritative old path while separately querying the new memory store without letting shadow results trigger publication, payments, notifications, or other external actions. Compare normalized evidence, provenance, security filters, and response quality. Raw document equality may be the wrong test if storage formats legitimately changed; compare a canonical representation and independently evaluate retrieval outcomes. Do not expose other tenants' data in diff logs. What happens to new writes during validation? A backfill can be perfect at the snapshot time but still be stale at cutover. Keep one authoritative write path, capture updates durably (for example through a transactional outbox or change data capture mechanism), apply them idempotently to the new representation, and measure replication/index lag. Before switching readers, confirm the new store has caught up to the agreed cutover watermark. Use revision aware writes and read routing so late old format events cannot overwrite newer records. Canary and go/no go checklist Run a small cohort of agents or workflows against the new memory schema. Compare accepted outputs, missed requirements, authorization failures, latency, and cost against the old path on comparable work. Require zero known cross tenant leaks or unsafe approvals ; treat any such finding as an immediate stop. Confirm unresolved exceptions have explicit owners and have not been silently declared migrated. Switch traffic progressively only after independent checks pass, then continue monitoring and keep a defined rollback route. A small passing canary is not proof of universal correctness; expand coverage and account for statistical uncertainty. LangGraph and Celery implementation In LangGraph , test resumed and fresh workflows separately. A valid checkpoint may reference a memory record whose schema or revision changed. Ensure nodes revalidate required evidence rather than assuming a checkpoint guarantees memory freshness. With Celery , run idempotent migration and verification tasks using stable record IDs, durable progress markers, and revision checks. Tasks may retry or arrive out of order; verification should detect stale, duplicated, or skipped updates. Keep legacy compatible consumers available until queued old version messages have drained. Difference from earlier lessons Q220 explains how to execute shared memory schema migration. Q221 explains how to maintain consistency while old and new memory coexist. This lesson defines the independent evidence and release gates required to declare the migrated system ready for agent cutover. In short Cut over only after record reconciliation, semantic and security validation, retrieval checks, live write catch up, agent integration tests, and a passing canary. A completed backfill is not the same as a verified migration.
How do you roll back a shared-memory migration safely after detecting data inconsistencies?
Rolling back a shared memory migration means restoring a trusted way for agents to read and write memory after the new schema or store is found to contain inconsistent data. The safest rollback is usually to stop using the faulty new representation, preserve evidence, and reconcile writes made since the migration started. Simply restoring yesterday's database backup could erase legitimate new information. Beginner friendly example: API test generation A requirements agent writes endpoint authentication rules to shared memory. A test generator and security reviewer retrieve them. A migration transforms auth: "OAuth2" into a structured record with scheme, scopes, provenance, and revision number. After switching some readers to the new store, an evaluation finds that the pay:write scope is missing for several endpoints. Generating tests from these records could silently omit authorization cases. Detect mismatch → Stop or limit affected reads/writes → Preserve state → Switch to a verified authority → Reconcile updates → Validate → Resume safely Numerical example: why rollback needs reconciliation Suppose 10,000 records were migrated: 1. 9,950 pass semantic checks, while 50 show missing or stale authorization data. 2. The observed discrepancy rate is 50 / 10,000 × 100 = 0.5% . Even a small rate can be unacceptable for security sensitive fields. 3. Since the migration snapshot, agents have made 120 legitimate updates to the authoritative data. 4. Restoring the old snapshot without replaying those updates could discard as many as 120 changes. 5. Instead, stop promotion of the new data, identify all changes after the snapshot watermark, and replay or reconcile the 120 updates onto the verified rollback target using stable event IDs and revision checks. After reconciliation, compare authoritative records and security critical fields again. The 50 inconsistent records must be explicitly repaired, quarantined, or routed to verified older data; do not assume reverting reader code makes them correct. Safe rollback procedure 1. Declare the scope and stop conditions: Record affected tenants, memory collections, schema versions, record revisions, and the first known inconsistency. Pause further cutover. 2. Contain unsafe behavior: Disable reads from suspect records or route them to verified data; suspend sensitive actions such as publishing test suites without authorization evidence. 3. Choose an authoritative state: Identify the last trusted snapshot, canonical write ledger, or original database. Do not treat two diverged writable stores as equally correct. 4. Preserve evidence: Retain pre migration backups, new store data, source revisions, change logs, and failure samples for investigation. Avoid writing secrets or full customer documents into ordinary logs. 5. Quiesce or coordinate writes: Temporarily gate affected updates or route all new writes through one known good path; use ownership checks to stop old/new workers racing. 6. Roll back reads separately from data: Where possible, switch readers to the compatible trusted store first. Keep incompatible writers disabled until their output contract is safe. 7. Replay post snapshot changes: Apply durable events in source revision order, with idempotent event IDs. Resolve conflicting or irreversible transformations manually where needed. 8. Rebuild derived representations: Invalidate caches and rebuild search/vector indexes from the reconciled authoritative records; do not serve stale index entries as if they were current. 9. Verify before resuming: Check record counts, field meaning, revisions, tenant isolation, retrieval results, and agent produced outputs. For authorization and approval fields, require zero known unsafe discrepancies. 10. Resume gradually and monitor: Canary a small cohort, watch mismatch counts and user visible validity, and keep a tested stop/recovery route. What if the new schema contains information the old one cannot store? A backward migration may be lossy . For example, v2 stores OAuth scopes but v1 has only a single authentication string. Converting back to "OAuth2" discards scopes. Options include keeping a compatible v2 extension/sidecar, preserving the new fields in an archive, or pausing writers until a lossless recovery format exists. Never silently delete required security metadata just to regain schema compatibility. Sometimes forward fixing the migration while keeping the old path available is safer than physically reversing the data transformation. How do you handle workflows already using migrated memory? Find affected workflow IDs and memory revisions. A paused workflow may hold a checkpoint or cached summary that references an inconsistent record. Revalidate its required evidence before continuing and quarantine already generated artifacts whose correctness is uncertain. Check published results and external side effects separately: a database rollback cannot undo an already delivered test suite, email, or external API action. LangGraph and Celery implementation In LangGraph , keep checkpoint schema and shared memory schema versions distinct. Switching a memory reader back does not automatically rewrite a paused checkpoint; revalidate evidence and pin compatible memory readers for resumed nodes where needed. With Celery , stop or reroute migration/backfill queues, account for tasks already reserved or executing, and make reconciliation tasks revision aware and idempotent. An old queued write must not overwrite newer authoritative data after rollback. Task revocation alone does not reverse an already committed external write. How does this differ from earlier lessons? Q216: Reverts a problematic workflow release or routing configuration. Q221: Keeps old and new shared memory consistent during migration. Q222: Decides whether a migration is ready for cutover . This lesson: Contains, reverses, or repairs a migration after discrepancies are detected, without losing post snapshot writes or propagating unsafe data. In short Rollback means restoring trustworthy behavior and consistent information—not merely deploying old code or loading an old backup. Preserve a single source of truth, reconcile intervening writes, revalidate affected workflows, and resume only after semantic and security checks pass.
How do you perform zero-downtime shared-memory migrations in multi-agent systems?
A zero downtime shared memory migration changes the storage schema or backing store while agents continue serving requests, without requiring a service wide maintenance shutdown. It does not mean every operation has zero latency or risk: it means old and new components coexist through a controlled transition, with measurable availability and correctness safeguards. Beginner friendly example: API test generation agents A requirements extractor writes endpoint facts to shared memory, while a test generator and security reviewer read them. The platform wants to replace an old auth: "OAuth2" field with structured authentication rules and evidence references. Stopping all agents for a database rewrite would interrupt API test generation. Instead, the service deploys readers that accept both formats , backfills existing records while requests continue, catches up live updates, and switches a small cohort of reads only after tests pass. Add compatible schema → Deploy tolerant readers → Backfill in small batches → Replicate live changes → Verify new reads → Canary cutover → Expand traffic → Retire legacy path Numerical example: migration without stopping requests Suppose the shared store holds 100,000 records , the backfill safely converts 1,000 records per minute , and agents continue creating or updating 100 records per minute . 1. The initial snapshot requires approximately 100,000 / 1,000 = 100 minutes to convert at the assumed throughput. 2. During those 100 minutes, roughly 10,000 live update events may occur. Not all are unique records. 3. A durable change stream captures these updates. The new representation is not ready for cutover merely because all 100,000 snapshot records were processed. 4. After catch up, imagine the new store trails the authoritative update stream by 20 events . If the cutover gate requires zero unapplied security sensitive updates , the switch must still wait. 5. Once it reaches the agreed watermark and all checks pass, route 5% of new eligible read traffic to the new schema and monitor quality and latency before expanding. These are illustrative values. Real migration duration depends on database capacity, read/write contention, transformation cost, and the ability to apply the live stream faster than new changes arrive. How do you implement the zero downtime transition? 1. Inventory readers and writers: List every agent, Celery worker, workflow checkpoint, query path, and retrieval index that uses shared memory. 2. Use expand–migrate–contract: Introduce new optional columns or versioned records without immediately deleting old fields; deploy compatible readers before changing producers. 3. Keep one write authority: Route accepted mutations through a single canonical path. If synchronizing stores, prefer an atomic transaction/outbox or dependable change stream; do not assume two independent writes always succeed together. 4. Backfill incrementally: Use bounded, idempotent batches with stable record IDs and conditional revision checks, avoiding long locks or heavy transactions that degrade live traffic. 5. Capture concurrent updates: Record a snapshot watermark and apply later events in a verifiable order per record. Prevent old migration tasks from overwriting newer data. 6. Shadow read and reconcile: Compare old/new data and retrieval outputs without letting shadow responses publish test suites or invoke external tools. 7. Verify permission boundaries: Test tenant isolation and authorization semantics. A structurally valid converted record can still be dangerous if its access rules changed. 8. Canary new readers: Route a small, well defined cohort to the new path; use feature flags or version aware routing and compare end to end outcomes. 9. Observe performance and correctness: Track p95/p99 read latency, write errors, backlog, replication lag, invalid records, and quality of agent outputs. 10. Retire old paths gradually: Drain old queue messages and compatible workers, preserve rollback options, and remove legacy fields only after the agreed retention and verification period. What does a safe cutover gate look like? Before increasing traffic, require: All required records are present or explicitly quarantined. No unresolved security sensitive field mismatches or cross tenant retrievals. Acknowledged updates are visible at or beyond the agreed change stream watermark. Latency, error rate, and memory retrieval quality meet defined thresholds. The team can immediately switch new reads back to the trusted path. In flight workflows continue on compatible versions or have a tested migration plan. For strong per request read after write guarantees, an eventual consistency mirror may be insufficient even if average lag is small. Use version aware read routing, wait for the required revision, or read from the authoritative store. How do you minimize service disruption? Throttle migration jobs based on live database load rather than maximizing backfill speed. Avoid blocking schema changes on large hot tables where possible; test the exact DDL and lock behavior. Keep schema changes backward compatible while old services run. Route reads away from the new store immediately if it violates correctness or latency gates. Keep temporary dual format support long enough for old agents, caches, and queued work to drain. Zero downtime does not promise zero degraded requests. Define allowed latency/error impact before starting and stop the rollout if those limits are breached. LangGraph and Celery implementation In LangGraph , a paused checkpoint may reference a memory record created in the old schema. Keep the checkpoint's schema version separate from the shared memory version, and verify that resumed nodes read the correct authoritative revision. Do not assume upgrading the memory store upgrades all checkpoints. In Celery , execute migration jobs as idempotent batches and monitor worker concurrency, queue depth, retries, and source revision conflicts. Tasks already reserved by an old worker may continue to write old payloads, so maintain compatible ingestion or route by message version until the old queue drains. How does this differ from earlier questions? Q220: How to change shared memory schema and backfill data. Q221: How to preserve consistency during the transition. Q222: How to validate readiness for cutover. Q223: How to recover after a faulty migration. This lesson: How to coordinate those safeguards while the live multi agent service keeps accepting and completing work without a planned outage. In short Zero downtime migration combines backward compatible schema changes, incremental backfills, durable live update capture, safe read routing, and progressive cutover. Preserve a trusted write authority and measure correctness as carefully as availability.
How do you monitor and prevent data drift in shared memory across multiple AI agents?
Shared memory drift occurs when the facts, summaries, metadata, or retrieved representations used by different AI agents gradually stop matching the current authoritative source or each other. It can happen even when the database schema is unchanged and every agent appears healthy. For example, one agent may update an API's authentication requirements while another continues retrieving an old summary or vector index entry. The risk is that different specialists make incompatible decisions from different versions of the same fact. Beginner friendly example: API test generation A requirements agent reads an API specification and writes the authentication rules into shared memory. A test generation agent uses that memory to create cases, while a security reviewer checks coverage. Yesterday the endpoint used an API key. Today its specification changes to OAuth2 with a required pay:write scope . The requirements agent updates the source of truth record, but the vector index and a cached summary still say API key . The test generator now creates outdated authentication tests. The memory record might be structurally valid, yet its content or retrieval result has drifted. Observe authoritative changes → Check freshness and semantic agreement → Flag drift → Refresh or quarantine affected memory → Revalidate downstream outputs Numerical example Suppose an automated check samples 200 shared memory records against their authoritative source documents: 184 records still match. 10 records contain outdated requirements. 6 records have inconsistent metadata or evidence references. The observed drift rate = (10 + 6) / 200 × 100 = 8% . The agreement rate = 184 / 200 × 100 = 92% . If the service has a warning threshold of 2% for noncritical fields, the sample clearly exceeds it. For authentication or tenant authorization facts, a single known unsafe discrepancy may justify quarantining the affected record immediately rather than waiting for an aggregate threshold. These figures are illustrative; a 200 record sample estimates a broader population only if sampling is representative. Track important fields by risk category instead of hiding them inside one overall score. What kinds of drift can occur? 1. Source to memory drift: The external API specification changes, but an agent's remembered facts do not. 2. Agent to agent divergence: Different agents hold contradictory summaries or local cached copies of the same requirement. 3. Summary drift: Repeated compression or rewriting removes important constraints or introduces unsupported details. 4. Index drift: Search/vector indexes lag behind the canonical document or carry stale metadata. 5. Semantic drift: A field retains its schema but its meaning changes, such as treating REVIEW PENDING as approval. 6. Permissions drift: Memory visibility or tenant ownership no longer matches current access policy. Do not confuse this with model/data distribution drift , which measures changes in statistical input or output distributions. Shared memory drift is primarily about factual freshness, consistency, provenance, retrieval correctness, and access policy . How do you detect it? Track provenance: Store a source ID, version or revision, ingestion timestamp, and validation status with each memory record. Compare authoritative revisions: Identify records whose source changed after the memory's verified revision. Audit semantic accuracy: Periodically compare sampled memory claims against original evidence; use deterministic validation when possible and independent review for ambiguous meaning. Check retrieval: Run golden queries with expected source references and verify that agents retrieve the correct current evidence. Monitor caches and indexes: Track refresh lag, index revision, missing records, and stale cache hit rates. Detect conflicts: Flag contradictory values for the same endpoint and scope instead of allowing last writer wins to silently pick one. Test authorization: Include negative checks that a different tenant cannot retrieve the record. For measurable operations, track record drift rate, critical field mismatch count, source to index lag, freshness age distribution, and incidents caused by stale memory . How do you prevent and correct drift? 1. Maintain one authoritative source: Treat retrieved summaries and caches as derived evidence, not independent sources of truth. 2. Refresh on change: Use source change events, scheduled reconciliation, or explicit invalidation when canonical requirements change. 3. Use version aware writes: Reject stale agent updates using source revisions or compare and swap checks. 4. Set risk based freshness policies: Shorter allowed age for frequently changing security or policy facts; longer for stable reference material. 5. Ground summaries in evidence: Retain source references and re extract after material changes rather than indefinitely summarizing summaries. 6. Quarantine uncertain records: Mark unsupported or conflicting memory as requiring review; prevent security sensitive decisions from relying on it. 7. Rebuild derived indexes selectively: Re embed changed content or refresh metadata as needed; verify index catch up before serving affected queries. 8. Recheck impacted workflows: Identify test suites generated from obsolete revisions and revalidate them where needed. LangGraph and Celery implementation In LangGraph , store memory references and their source revisions in workflow state. Before a critical node uses a remembered authorization rule, check whether that revision is still valid. A checkpoint can correctly restore execution while still referencing stale external memory. With Celery , run periodic reconciliation tasks that sample records, compare source revisions, and schedule idempotent re extraction/index refresh. Correlate update events with stable IDs so delayed or retried tasks cannot overwrite newer verified data. How does this differ from earlier lessons? Q74: Explains why stale memory harms an individual agent. Q194: Covers concurrency and consistency of shared state during ordinary updates. Q220–224: Cover schema migration, consistency, verification, rollback, and zero downtime cutover. This lesson: Continuously detects and prevents factual or retrieval drift after a multi agent memory system is operating normally, even without a schema change. In short Keep agent memory tied to authoritative, versioned evidence. Monitor freshness, semantic agreement, index lag, and access control correctness; automatically refresh safe records and quarantine uncertain or security critical conflicts before they affect final outputs.
How do you implement expiration and garbage collection for shared memory in multi-agent systems?
Shared memory expiration and garbage collection determine when an AI agent should stop using a stored memory and when obsolete records can be safely deleted. A time to live (TTL) is an expiration policy; garbage collection (GC) is the controlled cleanup process that removes records and derived copies after they become eligible for deletion. In multi agent systems, these policies must apply consistently to shared databases, caches, vector indexes, and agent specific projections. Expired does not always mean immediately deleted: reads should enforce expiry even while an asynchronous cleanup job is pending. Beginner friendly example: API test generation A requirements agent records an API's current authentication scheme. The test generator and security reviewer both retrieve that fact. A temporary API error response may be useful for only 15 minutes . A derived requirements summary might be revalidated every 24 hours . A published test suite audit record may require a longer retention period under an applicable business or legal policy. These are illustrative values, not universal retention recommendations. Different record types need different lifecycles. If a cached authentication rule has expired, the generator must refresh it from an authoritative source or return a clear blocked state rather than silently reusing it. Store record with provenance and expiry → Check validity on read → Refresh or reject expired data → Mark eligible records → Remove derived copies → Audit deletion Numerical example Suppose a shared memory store holds 10,000 records : 1. 2,000 are temporary tool result memories with a 1 hour TTL . 2. Of those, 600 have passed their expiration timestamp. 3. Only 500 are eligible for deletion because 100 are temporarily protected by an active documented retention or investigation hold. 4. The cleaner deletes 500 eligible canonical records in 5 batches of 100 , with matching cache and index cleanup queued. 5. The store now has 9,500 canonical records . The 100 expired but held records remain stored but must not be served as current facts merely because deletion is deferred. If index cleanup lags, the read path still filters expired record IDs or versions . Physical deletion and logical invalidation have separate completion checks. How should you design the lifecycle? 1. Classify memory types: Distinguish ephemeral observations, working facts, durable verified knowledge, user preferences, and audit artifacts. Do not assign one TTL to everything. 2. Store lifecycle metadata: Include record ID, tenant, source revision, created/verified timestamps, expires at, retention category, and deletion/hold status. 3. Choose an expiration clock: For source dependent facts, use last authoritative verification when appropriate; re reading a stale memory should not automatically extend its validity. 4. Enforce expiry at retrieval: Filter expired or revoked records before passing them to any agent, including through caches and vector search results. 5. Separate refresh from retention: A record can require freshness revalidation while still being retained for audit purposes. 6. Use deletion holds carefully: Respect legitimate preservation requirements, but prevent held or disputed records from influencing live decisions. 7. Delete every relevant derived copy: Cover database rows, cached summaries, vector entries, temporary artifacts, and indexes where technically and contractually required. 8. Make cleanup idempotent: Stable record IDs and deletion event IDs allow Celery retries without deleting an unrelated new record that reused a key. 9. Protect concurrent writers: Use version aware delete conditions so a cleaner operating on revision 4 cannot remove an updated revision 5. 10. Measure and audit: Track expired records still retrievable, cleanup lag, failed deletions, orphaned index entries, and evidence of completed purges. What if an agent is using a memory when it expires? Consider a workflow that retrieved an OAuth requirement at 09:59 when its expiry is 10:00 . A security sensitive action at 10:05 should check the current authoritative revision or a permitted freshness policy before publication. Do not assume that a fact was still valid merely because an earlier node read it. For long running workflows, store the memory version consumed and define where revalidation is mandatory. A checkpoint can preserve an old reference without extending the memory's validity. Soft expiration versus hard deletion Soft expiration: The fact is no longer eligible for normal retrieval, but it remains stored temporarily for refresh, debugging, or required retention. Hard deletion: The underlying record and relevant searchable or cached copies are actually removed, subject to backup lifecycle and retention rules. Refresh: The system obtains new authoritative evidence; it should not mark old unsupported information as fresh simply by changing its timestamp. Deletion of sensitive user or tenant data may require broader privacy and regulatory handling, including backups and downstream processors, beyond ordinary cache eviction. LangGraph and Celery implementation In LangGraph , add freshness and access checks at nodes that retrieve shared memory. Store memory IDs and revisions in workflow state, then revalidate before critical decisions or after a lengthy pause. Checkpointing preserves workflow state, not the validity of external memory. With Celery , schedule bounded cleanup and revalidation jobs. Use idempotent, revision checked deletes; separately retry cache/vector index cleanup and maintain a durable cleanup status. A failed cleanup task must not make expired records visible again. How does this differ from earlier lessons? Stale memory handling: Explains why outdated memories degrade agent decisions. Shared memory drift monitoring: Detects disagreement between memory and authoritative evidence. Expiration and garbage collection: Defines the operational lifecycle of records: when retrieval is blocked, when refresh occurs, and how physical cleanup is verified. In short Use risk based expiration policies, enforce expiry at read time, and run revision safe cleanup across every derived store. Treat freshness, retention, and deletion as separate decisions so agents never mistake obsolete memory for valid evidence.
How do you implement memory compaction in long-running multi-agent systems?
Memory compaction reduces the size of information that long running agents repeatedly carry or retrieve, while preserving the facts, decisions, unresolved tasks, and evidence they still need. In a multi agent workflow, compaction is an operational strategy for keeping context and shared memory payloads within size and cost limits—not a reason to discard authoritative records. Beginner friendly example: generating API test suites A requirements agent, test generator, and security reviewer exchange information about 100 API endpoints. Over many handoffs, the shared conversation accumulates tool logs, repeated status updates, superseded test drafts, and important decisions. Instead of sending the entire history to every agent, a coordinator creates a versioned compact working context containing: Verified endpoint requirements and original source references. Current test coverage and artifacts, by stable ID. Security constraints, open issues, and unresolved decisions. Which agent owns the next step and which validation gates remain. Pointers to immutable history for questions that require the full evidence. Collect state → Separate essential facts from transient chatter → Compact with source references → Validate invariants → Atomically publish new version → Continue agents using the validated context Step by step numerical example Assume a workflow carries 40,000 tokens of accumulated context into each of 6 upcoming agent calls . 1. Without compaction, repeated context contributes approximately 40,000 × 6 = 240,000 input tokens . 2. A compactor creates an 8,000 token working context, while keeping the full source history in a durable archive. 3. Six calls using that compact context contribute 8,000 × 6 = 48,000 input tokens . 4. The repeated context reduction is 192,000 tokens , or 80% . 5. If creating and validating the compact context consumes another 12,000 input/output tokens combined , the illustrative gross token reduction across the sequence is 180,000 tokens . Actual billing savings depend on input/output token rates, caching, and any extra retrieval or review calls. The compaction must still pass correctness checks. A cheaper prompt that silently removes a mandatory authorization scope is not an optimization. What information should survive? 1. Task identity and goal: Workflow ID, tenant, endpoint, user request, acceptance criteria, and current stage. 2. Authoritative facts: Verified requirements, source document versions, exact constraints, and provenance. 3. Decisions and rationale: Accepted architectural or test design choices, plus reasons needed for later audit. 4. Outstanding work: Open issues, pending approvals, failed checks, and next responsible agent. 5. Side effect history: References to saved artifacts, publications, tool actions, and idempotency keys. 6. Uncertainty and conflicts: Explicitly mark unknown or disputed statements instead of turning guesses into facts. Transient tool chatter, duplicate status messages, and superseded drafts can usually be omitted from the active context , provided records that must be retained remain recoverable. How do you implement safe compaction? Set a trigger: Compact when active context crosses a defined threshold, at a checkpoint, or before a large branch join; avoid repeatedly compacting after every small message. Use a structured compact schema: Prefer fields such as verified requirements, artifact refs, open issues, decisions, source versions, and summary version. Summarize with evidence: Create summaries from immutable source records, not only from earlier summaries; retain citations or record IDs. Validate before activation: Check required fields, authorization constraints, test counts, artifact versions, and unresolved issues against the original record. Use atomic version switching: Save the compacted context as a new revision and update the active pointer only if the underlying source revision has not changed. Keep recovery access: Preserve necessary raw evidence and allow agents to retrieve a relevant excerpt when the compact summary lacks detail. Test quality: Run representative endpoint tasks with and without compaction; track accepted suite quality, missed requirements, latency, and token cost. Enforce tenant boundaries: Never compact histories from unrelated customers into the same shared context. Numeric validation example Suppose the original record identifies 12 mandatory API constraints and 3 unresolved issues . A compacted context retains all 12 constraints but only 2 issues . Even if it saves thousands of tokens, it fails validation because one open issue disappeared. The compactor should reject that version, preserve the previous active context, and regenerate or request targeted review. Do not treat a summary as verified just because it is fluent or well formatted. How does compaction interact with parallel agents? Two agents may finish branches while a compactor is preparing a summary. If the active state advances from revision 7 to 8 , a compaction based on revision 7 must not overwrite revision 8. Use compare and swap, revisioned immutable summaries, or controlled merge operations; include both branches in a new validated compaction. Distinguish conversation context compaction from durable database or vector index compaction . Storage compaction may rewrite physical data layouts, whereas context compaction removes redundancy from what agents actively read. The integrity checks and operational trade offs differ. LangGraph and Celery implementation In LangGraph , compact at explicit checkpoint or coordination boundaries and preserve essential structured state separately from conversational messages. Treat existing checkpoints as durable records; a compact prompt does not automatically replace state or make older checkpoints compatible. With Celery , schedule compaction as an idempotent job keyed by workflow and source revision. Only publish the new context pointer after validation; stale or retried compaction tasks must not replace a newer revision. Keep a failed job from corrupting ongoing agent work. How is this different from earlier memory lessons? Q73 (memory consolidation): Explains turning multiple observations into reusable knowledge. Q226 (expiration and garbage collection): Defines when memories stop being retrievable and when obsolete records are deleted. This lesson: Optimizes the active working context of long running agent teams through versioned, verifiable compression while keeping essential evidence and durable state intact. In short Compact repeated agent context, not the truth. Preserve verified constraints, decisions, unresolved issues, and references to source evidence; validate each summary and switch versions atomically so cost savings never compromise the workflow's correctness.
How do you prevent information loss during memory compaction in multi-agent workflows?
Preventing information loss during memory compaction means proving that a shorter working context still contains every fact and decision needed to finish the workflow correctly. A readable summary is not enough: critical requirements, pending approvals, uncertainty, and references to original evidence must survive compression. In long running multi agent systems, the safest pattern is to keep authoritative state and source artifacts durable, compact only what agents repeatedly read, and reject any compacted context that fails validation . Beginner friendly example: API test generation A requirements agent extracts rules for POST /payments, a test generation agent writes cases, and a security agent reviews them. The original history contains: POST /payments requires OAuth2 with the pay:write scope. A successful request returns 201 . Missing authorization returns 401 . An unresolved issue: whether duplicate requests need an idempotency key. The security reviewer has not approved the generated tests. Requirements originate from specification revision v7 . Unsafe summary: “Payment tests were generated; ready to publish.” This loses the required scope, edge cases, unresolved issue, and approval status. A safe compact context preserves the exact constraints and keeps links to the original specification, test artifact IDs, and review records. Identify protected facts → Construct compact context → Compare with source → Run invariant checks → Validate downstream behavior → Activate only if all gates pass Step by step numerical example Suppose a workflow has 30 requirements , 5 unresolved issues , and 2 pending approvals . 1. The original history occupies 24,000 tokens . 2. The proposed summary occupies 4,000 tokens : an 83.3% context reduction , calculated as (24,000 − 4,000) / 24,000 × 100. 3. Automated extraction checks find 29 of 30 requirements , 4 of 5 issues , and both approvals . 4. Requirement recall is 29 / 30 = 96.7% ; open issue recall is 4 / 5 = 80% . 5. If the omitted requirement is an authorization scope or the omitted issue could affect publication, the candidate must fail despite the attractive token savings. 6. A second summary includes all 30 requirements , 5 issues , and 2 pending approvals , with evidence links. It can proceed to semantic and end to end validation; coverage alone does not prove every retained statement is accurate. These numbers are illustrative. For mandatory security constraints and workflow approval gates, aim for 100% verified preservation rather than averaging safety critical omissions into an overall score. Which information should never disappear silently? 1. User goal and acceptance criteria: Exact task scope, endpoints, constraints, and what constitutes completion. 2. Hard requirements: Authentication rules, numerical limits, contractual terms, security policies, and negative test expectations. 3. Decisions and ownership: Accepted choices, decision status, responsible agent, and next handoff. 4. Unresolved work: Open bugs, questions, failed tests, blocked steps, and pending human approvals. 5. Side effect ledger: Tool calls, published artifacts, external write IDs, and idempotency keys needed to avoid duplicate actions. 6. Uncertainty: Whether facts are verified, inferred, disputed, or unknown; never convert “pending review” into “approved.” 7. Evidence: Stable record IDs, source versions, timestamps, and retrieval pointers allowing exact details to be reconstructed. 8. Security boundaries: Tenant IDs, permitted scopes, sensitive data handling rules, and redaction requirements. Keep these as structured fields when possible, not only free form prose. How do you validate a compacted memory? Schema checks: Require all mandatory keys and correct types. Coverage checks: Compare source requirements, issue IDs, decisions, and approvals against the compacted representation. Entailment checks: Verify that each summary claim is supported by a cited source. The summarizer should not invent evidence. Contradiction checks: Flag mismatched values such as 401 becoming 403, or PENDING becoming PASS. Golden question tests: Ask known questions of both full and compacted contexts; compare exact constraints and supporting sources. Downstream regression tests: Generate representative test cases using each context and compare accepted coverage, unsafe omissions, and review outcomes. Retrieval fallback: Let agents fetch the full source section when a compact summary cannot answer a high stakes question confidently. No automated LLM evaluator is perfectly reliable. Combine deterministic field comparisons, representative end to end tests, and targeted human review for high risk changes. Avoid repeated summarization loss Repeatedly summarizing a summary can gradually remove rare but important details. Keep the immutable original history or authoritative artifacts available. Build periodic summaries from source evidence, or merge new observations with a versioned structured state, rather than compressing only the latest shortened text indefinitely. Use clear links such as source doc revision=v7, requirement ids=[R1,...], and pending approval id=A2. A reference is useful only if the referenced artifact remains available and authorized. What about concurrent multi agent updates? While a compactor reads revision 12 , a reviewer may finish a decision and advance shared state to revision 13 . Publishing the old summary unconditionally could erase the latest result. Create the summary against a source revision, validate it, and update the active summary pointer only if that revision remains current. If the compare and swap fails, retry against the latest state or incorporate the new event. Keep previous validated versions for recovery. LangGraph and Celery implementation In LangGraph , the checkpointer can retain state across thread steps, while a node selects which messages or summary to supply to the model. Keep full evidence in durable state or an authorized store and maintain a separate validated summary. Be careful: deleting messages from persisted graph state is different from excluding them from one model call. Preserve valid model/tool message boundaries if trimming. With Celery , run summarization and verification as separate idempotent jobs keyed by workflow ID and source revision. Never promote a failed summary. A late retry from an older revision must not overwrite a newer validated compact context. Difference from previous lessons Q73: Explains the general idea of consolidating repeated memories into reusable knowledge. Q227: Explains when and how to compact active context for cost and token efficiency. This lesson: Defines concrete preservation invariants, source grounding checks, omission metrics, and release gates that stop a lossy compaction from changing multi agent decisions. In short Treat compaction as a lossy transformation that must prove it has preserved what matters. Keep authoritative evidence, require complete coverage of critical fields and open work, test agent outcomes, and activate only revision safe summaries that pass validation. Further reading LangGraph: Add and manage memory LangMem: Managing long context with summarization LangGraph: Checkpointers
How do you evaluate the quality of compacted memory in multi-agent systems?
Evaluating compacted memory means measuring whether a shortened working context helps multiple agents complete their tasks correctly while reducing token use, cost, and latency. A compact summary is not high quality merely because it is shorter or readable. It must preserve essential facts, avoid unsupported claims, and yield reliable downstream decisions. A useful evaluation combines content level tests with task level tests . Content level tests check what survived compression; task level tests check whether the agents can still do their jobs. Beginner friendly example: an API test generation workflow Four agents process an API specification: requirements extraction, test generation, security review, and publication. They repeatedly share a long history. A compactor replaces repeated chatter with a short context containing verified endpoint rules, artifact references, unresolved issues, and pending approvals. Compare two versions on the same set of endpoint tasks : Baseline: Agents receive full, relevant historical context. Candidate: Agents receive compacted context plus access to original source records. Keep the dataset, model version, tool permissions, and evaluation rubric comparable. Otherwise apparent improvements might come from unrelated changes. Build reference cases → Define metrics → Evaluate preserved evidence → Run downstream agents → Compare quality and efficiency → Apply release gates Numerical example: calculating quality and savings Suppose a compacted context is evaluated against 40 required facts and contains 38 correctly preserved facts , 2 unsupported claims , and omits 2 required facts . 1. Critical fact recall: 38 / 40 = 95%. Two facts were omitted. 2. Supported claim precision: 38 / (38 + 2) = 95%. Two included claims lack authoritative support. 3. Unsupported claim rate: 2 / 40 = 5%, where the denominator is all 40 extracted claims in the compacted context. 4. Compression ratio: Original context 20,000 tokens ; compacted context 5,000 tokens . Ratio 20,000 / 5,000 = 4:1, with a 75% reduction in context tokens. 5. Task success: In 100 comparable endpoint cases, the full context workflow passes 94 , whereas the compacted workflow passes 90 . Observed success rates are 94% and 90% , a difference of −4 percentage points . 6. Critical omissions: If either missing fact is a mandatory security rule, mark the candidate NO GO , regardless of token reduction. These are illustrative, sample based metrics. A measured 4 point difference does not establish statistical significance without suitable paired case analysis and uncertainty estimates. Which quality metrics matter? 1. Requirement or fact recall: Fraction of reference facts retained. Separate mandatory security, business, and ordinary contextual facts. 2. Grounded precision or faithfulness: Fraction of summary claims supported by original evidence. Incorrect facts are different from missing facts and must be counted separately. 3. Contradiction rate: How often the compact context reverses a requirement, number, status, or decision. 4. Pending work preservation: Whether unresolved issues, owner assignments, approval gates, and open tool operations remain explicit. 5. Source traceability: Fraction of material claims that point to valid document versions, record IDs, or evidence spans. 6. Task success and regression rate: Whether the test generation and review agents still satisfy acceptance criteria after compaction. 7. Retrieval fallback quality: Whether agents can recover omitted but available details from authoritative records when necessary. 8. Efficiency: Input/output tokens, compaction overhead, actual monetary cost, end to end p50/p95 latency, and number of retrieval calls. Do not combine these into one score without hard constraints. A 90% overall score can conceal a security critical hallucination. How should you build an evaluation dataset? Select representative short, long, and very long workflows, not just easy examples. Include normal API endpoints, authentication sensitive endpoints, branching conversations, contradictory revisions, partial approvals, and recently changed requirements. Label the canonical facts, unresolved issues, and required source references directly from authoritative documents. Include adversarial cases where the earlier conversation contains wrong information later corrected. Add multi agent handoff cases where one agent's omitted detail causes another agent to fail. Split development cases from a held out regression set so the team does not tune only to remembered examples. For high impact facts, deterministic extraction and rule checks are especially valuable. LLM as judge or entailment checks can help detect unsupported claims, but should be calibrated against human reviewed examples and not treated as infallible. Practical paired evaluation For each test case, run both full and compact contexts under controlled conditions. Store: Original and compact context revisions. Ground truth requirement and issue IDs. Agent output artifacts and reviewer decisions. Missing, unsupported, and contradictory claims. Token consumption, latency, and retrieval behavior. Use paired per case differences , not just averages. If baseline succeeds and candidate fails on the same endpoint, inspect what evidence was lost or misrepresented. Re run stochastic workloads or hold random seeds/configuration fixed where feasible to reduce noise. Example release gate A team could set illustrative gates: 100% preservation of known critical authorization facts and pending approvals. Zero known unsupported security claims or cross tenant memory leakage . At least 99% factual coverage for other labeled required items. No unacceptable degradation in held out endpoint level task success. Measurable end to end cost reduction after including summarization and fallback retrieval. The numerical example fails the recall and task quality expectations even though compression saves tokens. The correct action is to revise the compactor and rerun the evaluation, not publish a lower quality summary. Monitor after deployment Offline success does not guarantee safety in production. Canary the compact context for a small cohort, monitor missing requirement incidents, clarification frequency, fallback retrievals, cost, and p95 latency, and compare against a suitable full context control. Preserve the last validated summary revision and a quick routing rollback. LangGraph and Celery considerations With LangGraph , evaluate what is actually passed to the model, not only what is persisted in checkpoints. A checkpointer can retain full thread state while a node includes only the compact summary and selected recent messages in model input. Test handoff and resume behavior at real graph boundaries. With Celery , run compaction evaluation as reproducible jobs keyed by workflow and source revision. Separate candidate generation from scoring, store per case results, and block promotion if a revised source makes the evaluation stale. Difference from nearby questions Q80: Evaluates whether agent memory is useful in general. Q227: Implements compaction for long running multi agent workflows. Q228: Protects required information from being lost. This lesson: Creates an evaluation protocol and quantitative release criteria comparing compacted and full context workflows for fidelity, downstream accuracy, and actual efficiency. In short Measure factual coverage, groundedness, contradictions, task success, and end to end cost together. Never trade away mandatory constraints or approvals for smaller prompts; promote compacted memory only when held out tests and production canaries demonstrate acceptable outcomes. Further reading LangGraph: Add and manage memory Microsoft: Evaluation metrics for generative AI
How do you choose when to trigger memory compaction in a long-running multi-agent workflow?
Trigger memory compaction when the next multi agent step is likely to exceed a safe context budget, when accumulated history becomes inefficient to reuse, or at a stable handoff boundary—provided the essential facts can be preserved. Waiting until the model rejects an oversized prompt is too late; compacting after every message can waste tokens and repeatedly degrade information. The key distinction is when to compact , not how to summarize or score a summary. A good trigger policy combines measured context pressure, workflow checkpoints, expected future growth, compaction cost, and the risk of losing critical detail. Beginner friendly example: API test generation A LangGraph workflow uses a requirements agent, test generator, security reviewer, and publisher to create tests for a large API. The first agent reads requirements, the second accumulates test drafts and tool results, and the reviewer adds findings. Each handoff can repeat thousands of tokens. Compaction becomes useful before an expensive next step that would otherwise reread old logs. But a security review should not begin with an unverified summary that omits the OAuth scope, failed test IDs, or pending approvals. Measure current context → Reserve space for the next node → Check trigger and safety gates → Compact at a stable boundary → Validate → Reuse the new summary Numerical example: choose a safe threshold Assume the model supports a 32,000 token context window. A production policy reserves: 4,000 tokens for system instructions and tool definitions. 5,000 tokens for the next retrieved documents and tool results. 3,000 tokens for the expected output and uncertainty. 2,000 tokens as an operational safety margin. The maximum budget available for previously accumulated conversation and working memory is therefore 32,000 − 4,000 − 5,000 − 3,000 − 2,000 = 18,000 tokens . 1. Current history: 15,000 tokens . Next step is expected to add 2,000 . Projected history is 17,000 : stay below the budget; compaction may be skipped. 2. Later history: 17,000 tokens . Next step may add 4,000 . Projected history is 21,000 , exceeding the 18,000 safe budget by 3,000 . Trigger compaction before that step. 3. A validated summary plus recent unsummarized messages takes 7,000 tokens . With the expected 4,000 extra, the projected history is 11,000 tokens , leaving 7,000 of the history budget. 4. If compaction costs 3,000 tokens and avoids replaying 10,000 tokens in each of the next 5 agent calls , the illustrative gross repeated context reduction is 50,000 tokens . After the compaction cost, the approximate net token reduction is 47,000 tokens , ignoring caching, fallback retrieval, and different pricing for inputs versus outputs. These values are examples, not recommended universal thresholds. Use the exact model and tool limits that apply to the deployment. Token estimates can be inaccurate for tool outputs, multimodal content, and nonstandard message formatting. Trigger strategies and when to use them 1. Absolute token threshold: Compact when active messages exceed a measured token count. Predictable, but the same number may be inappropriate for models with different context windows. 2. Fraction of context window: Trigger near a configured share of usable input capacity, after reserving room for tools, retrieval, and output. This adapts better across models. 3. Projected next step threshold: Trigger when current tokens + expected next step tokens safe history budget. This helps prevent a failure before a large tool result or handoff. 4. Message count threshold: Useful as a cheap early signal, but 20 short messages and 20 large tool responses have very different sizes. 5. Workflow boundary trigger: Compact after requirements extraction, at a validated checkpoint, or before a branch join or agent handoff. These boundaries offer stable data and clearer invariants. 6. Cost benefit trigger: Compact when expected future repeated context savings outweigh summary generation cost and risk. A task with one step left may not benefit. 7. Quality or relevance trigger: If retrieval quality degrades because earlier messages dominate the prompt, consider a targeted reduction even before the token limit. 8. Emergency guard: If the projected context is too large and safe compaction fails, use selective authorized retrieval, smaller inputs, or stop with an explicit error instead of silently dropping required evidence. A practical policy often combines a projected token budget and a safe checkpoint boundary. Do not delay a necessary context reduction past the model limit just to reach a preferred boundary. Add hysteresis to prevent repeated compaction A naive policy might compact at 18,000 tokens, produce a 17,500 token summary, and trigger again almost immediately. This causes extra model calls and repeated summarization loss. Instead, use hysteresis : a high trigger, a substantially lower post compaction target, and a minimum interval or minimum amount of new content before another compaction. Example: trigger when projected working context exceeds 18,000 tokens, but aim to reduce it to 8,000 tokens. Preserve the last recent messages and source pointers as part of that 8,000. The next compaction is considered only after meaningful new content accumulates or a hard safety threshold is approached. Which moments should not trigger compaction? During unresolved tool call pairs: Keep a model's tool invocation and corresponding tool response consistent, rather than leaving an orphaned call in the compact context. During active external side effects: Do not restructure context mid publication or while a non idempotent tool operation is pending. Before safeguarding source evidence: Archive required raw references and maintain a recovery path. When critical facts cannot pass validation: Do not publish a summary that loses approval state, authorization constraints, source versions, or open issues. When there is insufficient expected reuse: Compaction overhead may exceed savings for a workflow about to finish. Separate full durable records from the context actually supplied to the model . Trimming the latter does not require deleting the former. Example operational policy A coordinator can implement the following logic at each model call boundary: The pseudocode leaves out production specific error handling and synchronization. A real system must also record the source revision, tenant, candidate version, evaluation result, and active summary pointer. LangGraph and Celery integration In LangGraph , place a dedicated summarization or context preparation node before expensive model calls or after stable milestones. If using LangMem's SummarizationNode, distinguish max tokens before summary (when summarization begins) from max tokens (the result budget), and tune token counting for the deployed model. A checkpoint persists graph state; it does not mean all historical messages should be sent to every model. In LangChain agent middleware , summarization triggers can use token counts, message counts, or fractions of context size. These are configurable thresholds, not automatic guarantees that critical information will survive. Keep required structured state outside the compressed conversational text. With Celery , schedule expensive compaction as a retry safe job keyed by workflow ID and source revision. Before activating the result, verify that no newer agent update invalidated it. If the job is delayed, enforce a synchronous context budget guard at the next model call boundary; do not blindly proceed with an oversized context. What should be monitored? Track the number of compactions per workflow, tokens before and after, percentage of compactions accepted, generation cost, added latency, projected context overflow incidents, fallback retrieval calls, and downstream quality regressions. An excessive compaction rate can indicate poor target sizing; overflow errors suggest triggers are too late. Difference from earlier lessons Q201: Allocates token budgets across collaborating agents. Q227: Implements the compaction mechanism and version safe activation. Q228: Preserves important facts and constraints. Q229: Evaluates quality and efficiency. This lesson: Specifies trigger timing , predictive thresholds, hysteresis, workflow boundaries, and when compaction should be skipped. In short Trigger before the next step threatens the usable context budget, preferably at a safe workflow boundary, and avoid repeated compaction with a lower post compaction target. Use measured growth, expected reuse, and validation gates—not a fixed message count alone. Further reading LangMem: How to manage long context with summarization LangMem: Short term memory API reference LangChain: Built in agent middleware