AI agents: memory, state and reliability

AI agents: memory, state and reliability

Учебные материалы

Four different kinds of state

ConceptWhat it retains
Call contextTokens available to the model now
Long-term memoryApplication records across calls: facts, preferences and references
CheckpointProcess state and a point from which execution can resume
KV (Key–Value) cacheIntermediate attention key/value computations that accelerate generation

Clearing a KV cache does not erase application memory. Restoring a checkpoint does not undo an email or payment already sent.

AI agents: memory, state and reliability: mechanism diagram with an example

Memory: freshness and isolation

  • A thread connects messages belonging to a process or conversation. Sharing its identifier (ID) without user isolation can expose another user's history.
  • A summary compresses information but can omit numbers, exceptions and references. Preserve original evidence for important conclusions.
  • A year-old customer preference may be stale. Keep its source and date and verify that it still applies.
  • Reduce a large tool result to relevant passages, preserve references and identify omissions. Arbitrary truncation may remove a crucial condition.

Timeout: the outcome is unknown

A timeout means confirmation did not arrive in time. The server may have executed the request. An idempotency key identifies one logical operation so a service can recognize its retries.

  1. The application submits an order with key order-17.
  2. The service creates the order, but the response is lost. The client sees a timeout.
  3. The application checks status or retries with the same key under the service contract.
  4. The service returns the previous result instead of creating another order.

A new key on each retry makes them different operations. Protection depends on the service's retention period and rules. After checkpoint recovery, also check whether the external action already happened before the crash.

Retry, correct or stop

SituationAction
429 and Retry-AfterRespect the waiting period; bound retries and apply delay/backoff
Validation: unknown fieldCorrect the fields against the contract; waiting does not repair arguments
Steps produce no new resultEnforce step, time and cost budgets
The user cancelsStop pending actions; completed actions are not automatically undone
Retrieval is unavailable but sources are mandatoryReport the limitation and offer a retry; do not substitute guesses

Concurrent writes and dependencies

Two workers read counter 10 and each writes 11. The expected value was 12: one update is lost. An atomic increment, lock or write-version check can prevent this. A transaction needs suitable isolation and update logic.

In “create user → assign role,” the second step waits for the first step's ID. Check role validity and permission for both actions before execution.

Diagnostics and measurements

  • Tracing connects steps, tools, safely recorded arguments and outcomes within one run. It helps explain failures.
  • Evaluation measures quality against defined criteria across tasks. Having a trace does not establish quality.
  • P95 is a value at or below which roughly 95% of latencies fall; p99 covers roughly 99%. The mean can hide a slow tail.

Example: 99 requests take 1 second each; one takes 101 seconds. The mean is 2 seconds, although one user waited almost two minutes. Inspect the distribution, tail percentiles and individual slow traces.