Event-driven memory for AI agents with Oracle OKafka

When an agent finishes a task, the next session usually starts from zero. A transcript may exist somewhere, but no useful handoffs, preferences, or decisions make it into persistent, searchable memory.

A transcript records what happened. Durable memory is a smaller, curated set of information selected for a purpose, with a source, an access scope, and rules for correction and deletion. For example, a user-approved decision is likely to be useful in future sessions, while an incidental tool result or sensitive detail probably isn’t.

Memory curation and selection can take time. For agents using Oracle AI Database, OKafka and Transactional Event Queues (TxEventQ) enable asynchronous processing of memory candidates. A worker can extract memory candidates, apply filters and rules, and then store memories for retrieval. The invoking agent is able to respond without waiting for the asynchronous work to finish.

Just looking for code? Check out companion lab on GitHub.

Context, transcripts, and memory are distinct

Agents have a few, general types of state, working context, transcript history, and durable memory.

Transcripts are evidence of what the agent did: promoting transcripts to durable memory requires curation, ownership, retention, and governance rules.

For example, append-only conversation logs can be kept separate from curated JSON document memory and associated embeddings. An agent can query durable memory with vector similarity and full-text search, while its write-back tool stores handoffs/decisions as requested.

Add an event path for memory work

Asynchronous, event-driven design moves expensive work away from the main agent. An agent can finish its work while an asynchronous event consumer handles long-running work in the background, complete with retries, idempotency, and callbacks.

A possible event-driven memory flow looks like this:

  1. Capture an agent interaction: Create an event for a distinct unit of work, including identifiers like conversation ID, event ID, source transcript, timestamp, and other metadata. Apply access-control or redaction rules before publish.
  2. Publish the event: A producer (OKafka) writes the event to a TxEventQ topic, alongside related database operations in the same transaction.
  3. Process/extract memory candidates asynchronously: A consumer group reads agent events and applies filtering, extraction, deduplication, and conflict handling. LLMs and other controls can help identify facts, coupled with deterministic scope and policy validation. Depending on the type of processing you use, this step can use one or more consumers, with distributed routing and retries/idempotency.
  4. Store memory: Save approved memory documents with their metadata, including embedding and search index updates.
  5. Retrieve during a later conversation: Search the memory store using semantic and exact-match signals, apply authorization and scope filters, and return the supporting memory references to the agent.

Oracle OKafka allows the event stream and memory data to live in the same database. Each processing stage can commit event consumption, related database writes, and publish of the next event together on the consumer’s database connection. Failed transactions roll everything back back together.

Async fan-out for sub-agents

Event streaming decouples the agent from post-processing: sub-agents can subscribe and process the data while the main agent responds to the caller.

An initial agent can create an artifact (like a transcript record), publish that event to a TxEventQ topic in the database, and have one or more sub-agents asynchronously process the record. For example, sub-agent can be used to asynchronously extract candidates, deduplicate memories, and produce vector embeddings.

Reference Implementations

OKafka Agent Memory

The companion lab implements a simple agent memory flow with Oracle AI Database, OKafka, and OCI Generative AI.

Three event consumers handle transcript intake, memory extraction and admission, and embedding generation. Source transcripts remain separate from selected memories, which can be retrieved through an owner-scoped search API. An accepted transcript can finish without producing any memory.

OKafka Basics

These OKafka code samples cover the building blocks of this architecture, providing building blocks for memory processing:

  • The migrate-kafka-to-oracle walkthrough starts with a plain Kafka Java producer and consumer, then switches to OKafka and TxEventQ. Later steps add OSON serialization and database work in the consumer transaction.
  • The support-ticket-intelligence sample demonstrates an event-driven enrichment path. It inserts a ticket and publishes a TicketOpened event atomically; a consumer then creates searchable chunks and embeddings. The same event-and-consumer structure could trigger memory extraction after an agent interaction.
  • The txeventq-fraud-detection sample shows transactional production of OSON events and a consumer that uses its database connection while scoring and persisting decisions.
  • The oracle-database-kafka-apis integration tests exercise topic creation, production, consumption, and transactional producer and consumer behavior with Oracle AI Database Free and Testcontainers.
Diagram illustrating the building blocks for agent memory in Oracle AI Database, featuring AI agents, event streaming with TxEventQ, and curated memory for documents and search.
Oracle AI Database multi-model storage and event streaming form building blocks for agent work

If you like the samples, star the repository!

When does event-streaming fit agent workflows?

Event-streaming fits agent workloads when follow-up work is required on the generated transcripts, especially when this work is time consuming and requires stateful, durable execution.

For event-driven agent workflows, TxEventQ provides durable streaming: Oracle AI Database persists transcript data, curated memory documents, metadata, and vectors. OKafka gives familiar Java APIs to your agents to publish and consume events in the database, using database transactions.

Async work also means consistency tradeoffs: a background work might finish its work after the next agent begins a new task.

Further reading

Multi-model database and OKafka
Event streaming for agents

Leave a Reply