How Do You Build Event-Driven AI Agents on Kafka and Flink Instead of Batch Data?
A five-layer design for agents that react to live streams, with replay-based testing before anything reaches production
Agents do not need batch data. They can run on live streams if you treat Kafka as the durable log of record, Flink as the stateful processor that turns raw events into agent-ready context, and a replay harness as the way you test agent behavior before it touches production. Conduktor's write-up on Kafka and Flink for agents at scale makes the same case from the infrastructure side. Below we lay out a five-layer design, explain where replay-based testing fits, and say plainly which decisions should still wait for a batch job or a human.
Key Takeaways
- •An event-driven agent reads from a durable log, not a nightly snapshot, so its context is minutes or seconds old instead of a day old.
- •Kafka gives the agent a replayable history; Flink gives it keyed, stateful context such as rolling windows and joins.
- •Replaying recorded topics against a candidate agent is the most reliable way to test agent behavior before production.
- •Agent actions should leave the stream through a gated layer with an approval policy per domain, never straight from a consumer loop.
- •Batch data is still the right input for backfills, reconciliation and slow-moving dimensions.
Why batch data limits what an agent can do
A batch pipeline hands an agent a picture of the world as it was at the last run. If the job lands at 02:00, an agent answering at 14:00 reasons over data twelve hours old. For a question like "Why did row counts drop 40% overnight?" that is acceptable, because the answer is historical. For "Is the consumer lag growing right now?" it is useless, because the event you care about is still unfolding.
The second limit is coordination. Batch jobs produce tables, and tables do not tell an agent what changed or in what order. An agent that polls a table on a schedule has to diff state to rediscover events the system already knew about. An event log keeps the order and the cause, so the agent receives "schema version 4 registered at 09:12" instead of inferring it from a changed column list.
The third limit is cost of reaction. A polling agent either runs often and wastes compute on empty checks, or runs rarely and misses windows. A consumer subscribed to a topic only wakes when something happens. That does not make batch wrong. It means the trigger for an agent should be an event, and the heavy historical context should come from wherever it already lives.
The five layers of an event-driven agent
We structure these systems in five layers, and each one answers a different question: where do events live, how are they shaped, what does the agent remember, what can the agent call, and what is it allowed to do. Keeping the layers separate is what lets you swap a model, change a topic or tighten a policy without rewriting the rest.
- •Layer 1, the log: Kafka topics hold every event in order. This is the source of truth the agent reads and the history you replay.
- •Layer 2, the shape: a schema registry and data contracts decide what an event means, so a producer change cannot silently break the agent.
- •Layer 3, the context: Flink jobs build keyed state, windows and joins, and publish derived topics the agent consumes.
- •Layer 4, the tools: the agent calls MCP servers for lookups, lineage and diagnostics instead of embedding queries in prompts.
- •Layer 5, the gate: every write or remediation goes through an approval policy with an audit record.
Layer 1 is where the durability claim comes from. Kafka's broker setting log.retention.hours defaults to 168 hours, or seven days, which sets the default window you can replay without any extra tiering or archival. If your agent needs to be tested against events older than a week, raise retention or archive the topic, because you cannot replay what the broker has already deleted.
Layer 4 follows the Model Context Protocol specification, which defines how a client discovers and calls tools exposed by a server. Putting tool access behind MCP means the same agent logic works whether the question comes from a stream trigger or from an engineer in a terminal, and every call can be logged in one place.
Where Flink fits and where it does not
Flink is useful when the agent needs context that a single event cannot supply. A lone "payment failed" event says little. The count of failures per merchant over the last ten minutes, joined to the merchant's usual baseline, says a lot. Flink computes that kind of keyed, windowed state continuously and writes the result to a derived topic, so the agent consumes a ready-made signal instead of recomputing it in a prompt.
The Flink checkpointing documentation explains how state is snapshotted so a job can recover after a failure without losing or double counting events. That matters for agents because a duplicated or dropped signal turns into a duplicated or missed action. Treat the processing guarantee as part of the agent's safety story, not as a platform detail someone else owns.
Flink is not required for every case. If the trigger is a single event and the context is a lookup, a plain consumer plus an MCP tool call is simpler and has fewer moving parts. We add Flink when we need windows, joins across topics or state that outlives one message. Starting without it and adding it when a specific agent needs rolling context is a reasonable order of operations.
Replay-based agent testing
The strongest argument for a log-centric design is testing. Agents are non-deterministic, so unit tests on prompts catch little. A recorded topic gives you something better: a real sequence of events, in order, with the incidents and edge cases your system has actually produced. You point a candidate agent at that history in a sandbox, let it propose actions, and compare those proposals with what happened.
A workable replay loop has four steps. Pick a window that contains a known incident. Run the candidate agent against it with its action layer set to dry-run, so proposals are recorded but nothing executes. Score each proposal against what the on-call engineer did at the time. Then widen the window to include quiet periods, because the common failure of a new agent is acting when it should have stayed silent.
Replay also exposes drift. When you change a model, a prompt or a tool, rerun the same window and diff the proposed actions. A change that alters decisions on a window you already trust is a regression you can see before release. This only works if the topic, its schema versions and the retention policy are stable enough to reproduce the same input, which is why layers 1 and 2 come first.
Governing the stream: schemas, contracts and exactly-once
An agent that reacts to events inherits every schema change its producers make. If a field is renamed or a type is widened, the agent may keep running and quietly misread the payload. A schema registry with compatibility rules, plus a data contract that names the owner of each topic, turns that silent failure into a rejected publish. We cover the response options in our post on renaming a column without breaking consumers.
Delivery semantics need the same attention. The Kafka documentation on message delivery semantics describes at-most-once, at-least-once and exactly-once delivery and the idempotent producer and transactions that support the last. An agent that triggers a ticket, a rollback or a notification should be idempotent on the action side, so a redelivered event cannot cause a second action even when the platform guarantees are weaker than you assumed.
Access control belongs here too. Topics often carry PII, and an agent should read only the topics and fields its role allows. Apply the same grants you would give a human analyst, record every read and call, and keep the audit trail tamper-evident. A stream makes data movement fast, which also makes a missing control spread quickly.
How the Streaming Agent fits
The Streaming Agent covers Kafka, Pulsar and Kinesis topic governance, schema registry checks and exactly-once monitoring, and it is exposed as an MCP server. In practice an engineer asks it what is happening on a topic, and it answers from the same log your agents consume. That keeps the human view and the agent view of the stream consistent.
It chains with the schema and observability agents. When a contract changes on a topic, the Streaming Agent detects it, the Schema Agent maps what depends on it, and the observability side confirms whether lag or quality moved. We describe that handoff in orchestrating streaming, schema and observability agents together.
The open-source edition runs locally and is read-only by default, which suits layers 1 through 4: observe, diagnose and propose. Writes, approval workflows and audit trails sit in the paid editions, which is where layer 5 lives. Each domain gets its own autonomy setting, so you can let an agent act on a low-risk topic while a payments topic stays on human approval.
What should stay batch, and what should stay human
Keep batch for work that is about completeness rather than speed: backfills, month-end reconciliation, slow-changing dimensions and model training sets. A stream tells you what is happening. A batch job tells you what the final, corrected numbers are. Agents are most useful when they can use both, reacting to the stream and checking claims against the reconciled tables.
Keep humans on irreversible actions and on any decision with legal or financial consequence. An event-driven agent can detect a spike, correlate it with a deploy and draft a rollback, but pausing a payment topic or dropping a column should wait for approval until the agent has a track record in replay and in supervised production.
A sensible rollout is read-only first, then dry-run proposals, then supervised actions on one low-risk topic, then wider autonomy per domain as the replay scores hold. Each step is reversible, and each produces evidence you can show a security reviewer.
Frequently Asked Questions
Can Kafka and Flink run AI agents on live streams?
Yes, with a division of labor. Kafka stores and orders the events, Flink computes stateful context from them, and the agent consumes derived topics and calls tools through a defined interface. The model itself usually does not run inside Flink. It runs as a consumer or a service that Flink-produced signals trigger, which keeps the stream job fast and the agent logic independently testable.
Do agents need batch data at all?
They need it for completeness, not for reaction. Use streams to trigger and to supply recent context, and use batch tables for history, reconciliation and anything that must be corrected after the fact. An agent that can read both will give better answers than one limited to either.
How do you test an event-driven agent before production?
Replay a recorded topic window against the candidate agent with actions set to dry-run, then compare its proposals with what engineers actually did. Include quiet periods to catch unnecessary actions, and rerun the same window after every model, prompt or tool change to spot regressions.
How much event history can I replay?
It depends on retention. Kafka's default is seven days, as the broker configuration documentation states, so longer test windows need higher retention or an archive of the topic. Decide the window you need for testing before you set retention, not after an incident.