7 min read

spaCy Multi‑Agent Reasoning: Planning, Memory & Graphs

AI

ThinkTools Team

AI Research Lead

Introduction

The world of artificial intelligence has long been fascinated by the idea of autonomous agents that can reason, collaborate, and adapt to new information. While single‑agent systems have become increasingly sophisticated, the real challenge lies in orchestrating multiple agents that share knowledge, negotiate tasks, and learn from each other. In this tutorial we walk through the design of a multi‑agent reasoning framework built on top of spaCy, the popular natural‑language processing library. By integrating planning modules, reflective memory, and knowledge‑graph‑driven semantic reasoning, the system can tackle complex, dynamic problems that would be difficult for a single agent to solve.

The architecture we present is modular, allowing developers to swap out components or extend functionality without breaking the core pipeline. We begin by outlining the overall system structure, then dive into each sub‑system—planning, reflection, memory, and knowledge graphs—before showing how they interoperate during a typical task. Throughout the post we illustrate concepts with concrete code snippets and real‑world examples, such as coordinating a team of agents to answer a customer support query, or orchestrating a search‑and‑rescue mission in a simulated environment.

By the end of this guide you will have a working prototype that demonstrates how spaCy can serve as the backbone for an advanced multi‑agent reasoning system, and you will understand the trade‑offs involved in designing such a system from the ground up.

Main Content

Architectural Overview

At its core, the system consists of three layers: the agent layer, the orchestration layer, and the knowledge layer. Each agent is a lightweight process that receives natural‑language prompts, parses them with spaCy, and produces an action plan. The orchestration layer manages the lifecycle of agents, routes messages, and aggregates results. The knowledge layer houses a graph database that stores entities, relations, and historical interactions.

Agents expose a simple interface: receive(prompt) → plan. The orchestration layer invokes this method, collects the plan, and may trigger additional agents if dependencies arise. For instance, if Agent A’s plan requires a fact that Agent B knows, the orchestration layer will forward a request to Agent B and merge the response. This decoupled design ensures that adding a new agent type—say, a visual‑analysis agent—requires minimal changes to the rest of the system.

Planning Module

Planning is the engine that turns a natural‑language request into a sequence of executable steps. We employ spaCy’s dependency parser to extract intent and entities, then feed the result into a lightweight planner built on top of the networkx library. The planner represents the task as a directed acyclic graph where nodes are sub‑tasks and edges encode precedence constraints.

During planning, the system consults the knowledge graph to resolve ambiguities. For example, if the prompt mentions “the latest report”, the planner queries the graph for the most recent document node linked to the relevant domain. Once the plan is constructed, it is serialized into a JSON structure that the orchestration layer can dispatch to the appropriate agents.

Reflection and Memory Integration

Reflection allows agents to evaluate the outcome of their actions and adjust future behavior. Each agent maintains a local memory buffer that stores the last N interactions, including the prompt, the plan, the outcome, and any feedback. After executing a plan, the agent feeds the result back into the memory, where a simple transformer‑based model evaluates success metrics.

The memory module also supports long‑term retention by periodically summarizing recent interactions and persisting them to the knowledge graph. This summarization step uses spaCy’s EntityRuler and TextCategorizer to extract key facts and store them as nodes and edges. Consequently, agents can recall past experiences, avoid repeating mistakes, and build upon prior knowledge.

Knowledge Graphs and Semantic Reasoning

The knowledge graph is the heart of semantic reasoning. We use Neo4j as the backend, interfacing through the py2neo driver. Each node represents an entity—such as a person, product, or event—and each relationship encodes a semantic link, like “author_of” or “located_in”. By indexing entities with spaCy’s EntityLinker, the system can resolve synonyms and disambiguate homonyms.

Semantic reasoning is performed by traversing the graph to answer queries that cannot be resolved by local knowledge alone. For instance, if an agent needs to determine the best route between two cities, it queries the graph for the shortest path using Dijkstra’s algorithm. The graph also supports inference rules; a simple rule engine can deduce that if a person is a manager of a team, then the team is supervised by that person. These inferences are materialized as new edges, enriching the graph over time.

Inter‑Agent Communication

Communication is orchestrated through a lightweight message bus built on top of ZeroMQ. Each agent subscribes to topics relevant to its domain, such as customer_support or logistics. When an agent publishes a message, the bus routes it to all interested parties. The orchestration layer acts as a mediator, ensuring that messages are authenticated, logged, and that the system’s state remains consistent.

To illustrate, imagine a scenario where Agent X needs to verify the availability of a product. It publishes a product_query message. Agent Y, which manages inventory, receives the message, queries its local database, and publishes a product_response. Agent X then incorporates the response into its plan. This publish‑subscribe pattern scales naturally as more agents join the ecosystem.

Putting It All Together

The final step is to integrate all components into a cohesive pipeline. We start by initializing the spaCy pipeline with custom components for entity linking and intent classification. Next, we spin up the Neo4j graph and populate it with seed data. Then, we launch a pool of agents, each listening on its designated topic. The orchestration layer runs an event loop that listens for incoming prompts, dispatches them to the appropriate agent, collects the resulting plans, and triggers any dependent agents.

During execution, agents continuously update their memory buffers and the knowledge graph. The reflection module evaluates outcomes and feeds back into the planning module, creating a closed‑loop system that learns over time. By the end of the tutorial, you will have a fully functional prototype that demonstrates how spaCy can be leveraged to build a sophisticated multi‑agent reasoning system capable of planning, reflecting, remembering, and reasoning over a knowledge graph.

Conclusion

Designing a multi‑agent reasoning system is a complex endeavor that requires careful consideration of architecture, data flow, and learning mechanisms. By leveraging spaCy’s robust NLP capabilities, a graph database for semantic reasoning, and lightweight orchestration tools, we can build a system that not only performs tasks efficiently but also adapts and improves through reflection and memory. The modular design presented here ensures that new agent types can be added with minimal friction, making the framework suitable for a wide range of applications—from customer support automation to autonomous logistics.

The key takeaways are that planning must be grounded in a semantic understanding of the task, reflection provides the feedback loop necessary for continuous improvement, and a knowledge graph offers a powerful way to store and reason about complex relationships. Together, these components create a resilient, intelligent ecosystem that can tackle real‑world problems with a level of sophistication previously reserved for single‑agent systems.

Call to Action

If you’re excited to experiment with multi‑agent systems, start by cloning the repository we’ve shared on GitHub and running the demo. Try extending the agent pool with a new domain—perhaps a financial advisor or a medical triage bot—and observe how the orchestration layer adapts. Share your findings on Twitter or LinkedIn using the hashtag #spaCyAgents, and let’s build a community of practitioners who push the boundaries of collaborative AI. Happy coding!

We value your privacy

We use cookies, including Google Analytics, to improve your experience on our site. By accepting, you agree to our use of these cookies. Learn more