Formalized Structures: The Algebra of Agentic Architectures
Mathematical Formalization of Tool-Using AI Systems
Robert Cunningham
AI Engineer & Consultant
Agentic Architecture: A Graph-Theoretic Framework
Introduction
This paper presents a mathematical framework for modeling interactions between users, AI agents, and tools as directed acyclic graphs (DAGs). We formalize how agent interactions are structured, stored, and analyzed through a graph-theoretic lens, where nodes contain input-output pairs and edges represent invocation relationships. This approach simplifies the representation while maintaining complete information about the interaction history.
Core Definitions
Definition (Agent System): Let be an agent with system prompt and toolset .
Definition (Interaction Cycle): A cycle represents a complete user-agent interaction, initiated by user input and concluded with agent response . We denote:
Definition (Chat History): The chat history after cycles is:
Derivative Chat Histories
When an agent receives input and determines tool should be called, it constructs query yielding:
The agent interprets this result via its internal chat method :
Definition (Derivative History): The first derivative of chat history includes intermediate tool interactions:
The second derivative captures the full depth of all nested interactions, including agent-to-agent invocations through tools.
Graph-Theoretic Representation
Node-Based Model
We represent each interaction as a node containing an input-output pair:
Definition (Interaction Node): A node in the interaction graph is defined as:
where is the input, is the output, and is a label identifying the processing entity (agent or tool).
Cycle as DAG
Each cycle forms a directed acyclic graph where:
- is the set of interaction nodes
- represents invocation relationships
- The root node contains labeled with agent
- Child nodes contain tool or sub-agent interactions
Proposition (DAG Property): For any cycle , the graph is acyclic, i.e., there exists no sequence of edges forming a directed cycle.
Proof: By construction, edges represent invocation relationships with strict temporal ordering. A tool cannot invoke its calling agent within the same execution context, ensuring acyclicity. ∎
Formal Graph Examples
Consider the following cycle types expressed as graphs:
Simple Input-Output (Type 1)
A simple cycle with no tool calls, just user input and agent response:
Traditional Flow:
u₁ → [A] → v₁
Node Representation:
[(u₁, v₁)ₐ]
Single Tool Call (Type 2)
Agent receives input, calls a tool, and responds:
Traditional Flow:
u₂ → [A] ─q→ [T] ─r→ [A] → v₂
Node Representation:
[(u₂, v₂)ₐ]
↓
[(q, r)ₜ]
where and
Nested Agent Invocation (Type 3)
Tool invokes another agent during execution:
Traditional Flow:
u₃ → [A] ─q→ [T] ─u'→ [B] ─v'→ [T] ─r→ [A] → v₃
Node Representation:
[(u₃, v₃)ₐ]
↓
[(q, r)ₜ]
↓
[(u', v')ᵦ]
Multiple Tool Calls (Type 4)
Agent calls two different tools before responding:
Traditional Flow:
u₄ → [A] ─q₁→ [T₁] ─r₁→ [A]
└─q₂→ [T₂] ─r₂→ [A] → v₄
Node Representation:
[(u₄, v₄)ₐ]
↙ ↘
[(q₁, r₁)ₜ₁] [(q₂, r₂)ₜ₂]
Matrix Representations
Tool Selection Matrix
For a cycle with tool calls across available tools, we define the selection matrix:
where if the -th call invokes tool , and otherwise.
Lemma (Row Sum Property): Each row of sums to exactly 1:
Access Control Matrix
In multi-agent systems, we formalize access control through a block matrix :
where:
- is the agent-to-agent adjacency matrix
- is the agent-to-tool access matrix
- is the set of agents, is the set of tools
Definition (Agent Dispatch System): A dispatch system is a tuple where:
- is the agent set
- is the tool set
- is the dispatch tool
- is the user-facing agent
- encodes access relations
The dispatch tool mediates inter-agent communication: agent can invoke agent via .
Reachability and Path Generation
Definition (Reachability): Agent is reachable from agent if for some , where denotes the -th power of matrix .
The set of all reachable agents from is:
Theorem (Transitive Closure): The transitive closure encodes all reachability relations, where iff .
Loop Prevention Through Nilpotency
Definition (Nilpotent Matrix): A matrix is nilpotent if such that . The smallest such is the nilpotency index.
Theorem (Loop-Free Characterization): An agent dispatch system is loop-free if and only if its agent-to-agent adjacency matrix is nilpotent.
Proof:
(⇒) Suppose the system is loop-free. Then the directed graph where is acyclic. For an acyclic graph with vertices, any path has length at most . Thus counts paths of length from to , which must be zero for all . Hence .
(⇐) Suppose for some . Then no paths of length or greater exist in the dispatch graph. If a cycle existed, we could traverse it repeatedly to create arbitrarily long paths, contradicting . Thus the system is loop-free. ∎
Corollary (Trace Test for Acyclicity): A dispatch system has no self-loops of length if and only if .
Proof: The trace counts closed walks of length . These exist iff there are cycles in the graph. ∎
Proposition (Maximum Invocation Depth): For a loop-free dispatch system with nilpotency index , the maximum invocation chain length is .
Example: Five-Agent System
Consider the access control matrix:
A B C D E | 0 1 2 3 4 5 6
A 0 0 0 1 1 | 1 1 0 0 0 0 0
B 0 0 0 0 0 | 0 0 1 1 0 1 0
C 0 1 0 0 0 | 1 0 0 0 0 0 1
D 0 0 1 0 0 | 1 0 0 0 0 0 0
E 0 0 0 0 0 | 0 1 0 0 1 0 0
The agent-to-agent submatrix is:
Computing powers:
Since with for all , the system is provably loop-free with maximum invocation depth 3.
Database Schema Formalization
Trace Record Structure
Each node in the persistent storage is represented as a tuple:
where:
- : unique identifier
- : reference to parent node
- : cycle grouping
- : sibling ordering
- : symbolic function name
- : JSON input/output
- : exception data (if any)
- : prompt versions
- : application version
- : creation and update timestamps
Tree Reconstruction
Given traces with the same , we reconstruct the DAG:
Theorem (Unique Reconstruction): The parent-child relationships encoded in fields uniquely determine a DAG structure for each cycle.
Proof: The parent_id field creates a forest structure. Within each cycle_id group, exactly one node has parent_id = NULL (the root), and all other nodes have exactly one parent, forming a tree (which is a special case of a DAG). ∎
Algebraic Properties
Composition of Cycles
The full interaction history over cycles can be viewed as a forest:
where each is a tree rooted at the user-agent interaction node.
Path Analysis
For any node in cycle , the depth is the length of the unique path from the root to :
Definition (Interaction Depth): The depth of a cycle is:
This represents the maximum nesting level of tool/agent invocations within the cycle.
Query and Result Arrays
For total tool calls across all cycles, we maintain:
These can be indexed by a mapping function where represents the -th tool call in cycle and is the global index.
Tensor Representation
For advanced analysis, we construct a 3-tensor:
where:
- = number of cycles
- = maximum nodes in any cycle
- = number of available tools
Element if the -th node in cycle invokes tool , and otherwise.
Proposition: Complete Reconstruction
Proposition: The quadruple where:
- is the chat history
- is the query array
- is the selection matrix
- is the result array
is necessary and sufficient to reconstruct the complete interaction history including all tool invocations and their relationships.
Proof:
Necessity: Each component captures essential information that cannot be derived from the others.
Sufficiency: Given :
- provides user inputs and agent outputs for each cycle
- identifies which tools were called and in what order
- provides the queries sent to each tool
- provides the results from each tool
The temporal ordering implicit in the arrays, combined with the selection matrix, allows complete reconstruction of the interaction DAG. ∎
Advantages of the Graph Model
- Simplified Storage: Each node stores a complete interaction pair, reducing edge complexity
- Natural Hierarchy: Parent-child relationships directly model invocation patterns
- Efficient Queries: Tree structure enables fast traversal and analysis
- Version Tracking: Node-level version information supports evolution analysis
- Composability: Cycles compose naturally as a forest of trees
Conclusion
This graph-theoretic framework provides a rigorous foundation for understanding and implementing agent-tool-user interaction systems. By representing interactions as DAGs with node-stored data pairs, we achieve both mathematical clarity and practical efficiency. The framework naturally supports persistence, analysis, and evolution of complex agent systems while maintaining the complete information needed for auditing and optimization.
The shift from edge-based to node-based storage of interaction data represents a key insight: by bundling input-output pairs within nodes, we transform a complex multi-graph into a simple tree structure, dramatically simplifying both theoretical analysis and practical implementation.