Event-Driven Agent Architecture
Overview
This document details the Event-Driven Architecture (EDA) for the light-genai-4j agent system. This architecture decouples agent invocation from execution, enabling high scalability, resilience, and asynchronous processing suitable for enterprise workloads.
1. Core Architecture
While SQL defines what a skill is (see Agent Skill Design), the implementation for enterprise usage leverages an Event-Driven Architecture (EDA). This decouples the agent requesting the skill (the “invoker”) from the agent or service executing the skill (the “worker”).
1.1 Core Components
- Invoker Agent: The agent that decides to call a tool/skill.
- A2A Service (
genai-agentic-kafka): The bridge between the synchronousAgentExecutorand the asynchronous message bus. - Topic Topology:
agent-commands: Topics where agents publish intent/skill execution requests.agent-events: Topics where agents/workers publish completion events.
1.2 Execution Flow
- LLM Decision: The LLM outputs a
ToolExecutionRequest. - Interception: The
AgentExecutor’sA2AServiceimplementation intercepts this request. - Command Emission:
- Instead of executing a Java method directly, it constructs a
SkillInvocationEventcontaining:correlationId: Unique ID for this interaction.skillName: Name of the skill to execute.arguments: JSON payload of arguments.replyTo: Topic to send the result to.
- This event is published to the
agent-commandstopic.
- Instead of executing a Java method directly, it constructs a
- Async Wait: The
AgentExecutorreturns anAsyncResponse(aCompletableFuture) and suspends the agent’s execution thread (virtually, if using Virtual Threads). - Worker Execution:
- A subscribed “Skill Worker” (which could be another Generic Agent or a dedicated microservice) picks up the event.
- It executes the logic (DB query, API call, calculation).
- Response Emission:
- The worker publishes a
SkillCompletionEventto thereplyTotopic. - Payload includes
correlationIdand the result/error.
- The worker publishes a
- Resumption:
- The original
A2AServiceconsumes the completion event. - It matches the
correlationIdand completes the pendingCompletableFuture. - The Agent resumes generation with the tool output.
- The original
1.3 Benefits
- Scalability: Heavy skills (e.g., “Generate Report”) don’t block the lightweight Agent Commander.
- Resilience: If the Worker is down, the command persists in Kafka.
- Decoupling: Agents don’t need to know the network location of skills.
2. Relationship with MCP (Model Context Protocol)
With this design, the agent system does not require an MCP server architecture. The skills table allows direct invocation of any capability (Java code, REST APIs, GraphQL, scripts) without the overhead or complexity of the MCP protocol.
2.1 Comparison
| Feature | Light GenAI 4j Design | MCP Server Architecture |
|---|---|---|
| Execution | In-process (Java) or Event-Driven | Networked JSON-RPC |
| Latency | Near-zero (local) / Async (EDA) | HTTP/Network round-trip |
| Control | Full SQL schema & validation | Remote server definition |
| Complexity | Low (Unified DB/Kafka) | High (Separate servers/processes) |
| Ecosystem | Custom integrations | Community-maintained tools |
3. Implementation Guidelines
3.1 Dependencies
<!-- pom.xml -->
<dependency>
<groupId>com.networknt</groupId>
<artifactId>genai-agentic-kafka</artifactId> <!-- Future Module -->
<version>${project.version}</version>
</dependency>
3.2 Design Principles
- Asynchrony: Default to async/event-driven for any I/O heavy skill.
- Correlation: Use
correlationIdrigorously to track request/response pairs across the bus. - Idempotency: Workers should handle duplicate events gracefully.