Hillcrest Ski & Sports
Technical Deep DiveMay 11, 202415 min read

Formalized Structures: The Algebra of Agentic Architectures

Mathematical Formalization of Tool-Using AI Systems

RC

Robert Cunningham

AI Engineer & Consultant

#Mathematics#Agentic AI#Formal Methods#Tensor Operations

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 AA be an agent with system prompt sAs_A and toolset TA={T1,T2,,Tn}T_A = \{ T_1, T_2, \dots, T_n \}.

Definition (Interaction Cycle): A cycle cic_i represents a complete user-agent interaction, initiated by user input uiu_i and concluded with agent response viv_i. We denote:

ci=(ui,vi)c_i = (u_i, v_i)

Definition (Chat History): The chat history after kk cycles is:

hk=[ci]i=1k=[(u1,v1),(u2,v2),,(uk,vk)]h_k = [c_i]_{i=1}^k = [(u_1, v_1), (u_2, v_2), \dots, (u_k, v_k)]

Derivative Chat Histories

When an agent AA receives input u1u_1 and determines tool TiT_i should be called, it constructs query q1(i)q_1^{(i)} yielding:

r1(i)=Ti(q1(i))r_1^{(i)} = T_i(q_1^{(i)})

The agent interprets this result via its internal chat method chat\text{chat}^*:

v1=chat(r1(i))v_1 = \text{chat}^*(r_1^{(i)})

Definition (Derivative History): The first derivative of chat history includes intermediate tool interactions:

h=[ci] where ci=(ui,qi(j),ri(j),vi)h' = [c'_i] \text{ where } c'_i = (u_i, q_i^{(j)}, r_i^{(j)}, v_i)

The second derivative hh'' 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 NN in the interaction graph is defined as:

N=(ein,eout,λ)N = (e_{\text{in}}, e_{\text{out}}, \lambda)

where eine_{\text{in}} is the input, eoute_{\text{out}} is the output, and λ\lambda is a label identifying the processing entity (agent or tool).

Cycle as DAG

Each cycle cic_i forms a directed acyclic graph Gi=(Vi,Ei)G_i = (V_i, E_i) where:

  • ViV_i is the set of interaction nodes
  • EiVi×ViE_i \subseteq V_i \times V_i represents invocation relationships
  • The root node contains (ui,vi)(u_i, v_i) labeled with agent AA
  • Child nodes contain tool or sub-agent interactions

Proposition (DAG Property): For any cycle cic_i, the graph GiG_i 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₁)ₐ]
G1=({NA},) where NA=(u1,v1,A)G_1 = (\{N_A\}, \emptyset) \text{ where } N_A = (u_1, v_1, A)

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)ₜ]
G2=({NA,NT},{(NA,NT)})G_2 = (\{N_A, N_T\}, \{(N_A, N_T)\})

where NA=(u2,v2,A)N_A = (u_2, v_2, A) and NT=(q2,r2,T)N_T = (q_2, r_2, T)

Nested Agent Invocation (Type 3)

Tool TT invokes another agent BB during execution:

Traditional Flow:
    u₃ → [A] ─q→ [T] ─u'→ [B] ─v'→ [T] ─r→ [A] → v₃

Node Representation:
    [(u₃, v₃)ₐ]
         ↓
      [(q, r)ₜ]
         ↓
     [(u', v')ᵦ]
G3=({NA,NT,NB},{(NA,NT),(NT,NB)})G_3 = (\{N_A, N_T, N_B\}, \{(N_A, N_T), (N_T, N_B)\})

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₂)ₜ₂]
G4=({NA,NT1,NT2},{(NA,NT1),(NA,NT2)})G_4 = (\{N_A, N_{T_1}, N_{T_2}\}, \{(N_A, N_{T_1}), (N_A, N_{T_2})\})

Matrix Representations

Tool Selection Matrix

For a cycle with mm tool calls across nn available tools, we define the selection matrix:

S{0,1}m×nS \in \{0,1\}^{m \times n}

where Sij=1S_{ij} = 1 if the ii-th call invokes tool TjT_j, and 00 otherwise.

Lemma (Row Sum Property): Each row of SS sums to exactly 1:

j=1nSij=1i{1,,m}\sum_{j=1}^n S_{ij} = 1 \quad \forall i \in \{1, \ldots, m\}

Access Control Matrix

In multi-agent systems, we formalize access control through a block matrix A\mathcal{A}:

A=[MR]\mathcal{A} = [M \mid R]

where:

  • M{0,1}A×AM \in \{0,1\}^{|A| \times |A|} is the agent-to-agent adjacency matrix
  • R{0,1}A×TR \in \{0,1\}^{|A| \times |T|} is the agent-to-tool access matrix
  • AA is the set of agents, TT is the set of tools

Definition (Agent Dispatch System): A dispatch system is a tuple (A,T,T0,A0,A)(A, T, T_0, A_0, \mathcal{A}) where:

  • A={a1,,an}A = \{a_1, \ldots, a_n\} is the agent set
  • T={t0,t1,,tm}T = \{t_0, t_1, \ldots, t_m\} is the tool set
  • T0={t0}TT_0 = \{t_0\} \subseteq T is the dispatch tool
  • A0={a1}AA_0 = \{a_1\} \subseteq A is the user-facing agent
  • A\mathcal{A} encodes access relations

The dispatch tool t0t_0 mediates inter-agent communication: Mij=1    M_{ij} = 1 \iff agent aia_i can invoke agent aja_j via t0t_0.

Reachability and Path Generation

Definition (Reachability): Agent aja_j is reachable from agent aia_i if (Mk)ij>0(M^k)_{ij} > 0 for some k1k \geq 1, where MkM^k denotes the kk-th power of matrix MM.

The set of all reachable agents from aia_i is:

R(ai)={aj:k1,(Mk)ij>0}\mathcal{R}(a_i) = \{a_j : \exists k \geq 1, (M^k)_{ij} > 0\}

Theorem (Transitive Closure): The transitive closure M=k=1n1MkM^* = \sum_{k=1}^{n-1} M^k encodes all reachability relations, where (M)ij>0(M^*)_{ij} > 0 iff ajR(ai)a_j \in \mathcal{R}(a_i).

Loop Prevention Through Nilpotency

Definition (Nilpotent Matrix): A matrix MM is nilpotent if kN\exists k \in \mathbb{N} such that Mk=0M^k = 0. The smallest such kk 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 MM is nilpotent.

Proof:

(⇒) Suppose the system is loop-free. Then the directed graph G=(A,E)G = (A, E) where E={(ai,aj):Mij=1}E = \{(a_i, a_j) : M_{ij} = 1\} is acyclic. For an acyclic graph with nn vertices, any path has length at most n1n-1. Thus (Mn)ij(M^n)_{ij} counts paths of length nn from aia_i to aja_j, which must be zero for all i,ji,j. Hence Mn=0M^n = 0.

(⇐) Suppose Mk=0M^k = 0 for some kk. Then no paths of length kk or greater exist in the dispatch graph. If a cycle existed, we could traverse it repeatedly to create arbitrarily long paths, contradicting Mk=0M^k = 0. Thus the system is loop-free. ∎

Corollary (Trace Test for Acyclicity): A dispatch system has no self-loops of length kk if and only if tr(Mk)=0\text{tr}(M^k) = 0.

Proof: The trace tr(Mk)=i=1n(Mk)ii\text{tr}(M^k) = \sum_{i=1}^n (M^k)_{ii} counts closed walks of length kk. These exist iff there are cycles in the graph. ∎

Proposition (Maximum Invocation Depth): For a loop-free dispatch system with nilpotency index ν\nu, the maximum invocation chain length is ν1\nu - 1.

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:

M=(0001100000010000010000000)M = \begin{pmatrix} 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{pmatrix}

Computing powers:

M2=(0010000000000000100000000),M3=(0100000000000000000000000),M4=0M^2 = \begin{pmatrix} 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{pmatrix}, \quad M^3 = \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{pmatrix}, \quad M^4 = 0

Since M4=0M^4 = 0 with tr(Mk)=0\text{tr}(M^k) = 0 for all kk, 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:

τ=(id,parent_id,cycle_id,call_order,fn,ein,eout,ϵ,pv,av,tc,tu)\tau = (\mathrm{id}, \mathrm{parent\_id}, \mathrm{cycle\_id}, \mathrm{call\_order}, \mathrm{fn}, e_{\mathrm{in}}, e_{\mathrm{out}}, \epsilon, \mathrm{pv}, \mathrm{av}, t_c, t_u)

where:

  • idN\mathrm{id} \in \mathbb{N}: unique identifier
  • parent_idN{NULL}\mathrm{parent\_id} \in \mathbb{N} \cup \{\mathrm{NULL}\}: reference to parent node
  • cycle_idN\mathrm{cycle\_id} \in \mathbb{N}: cycle grouping
  • call_orderN\mathrm{call\_order} \in \mathbb{N}: sibling ordering
  • fn\mathrm{fn}: symbolic function name
  • ein,eoute_{\text{in}}, e_{\text{out}}: JSON input/output
  • ϵ\epsilon: exception data (if any)
  • pv\mathrm{pv}: prompt versions
  • av\mathrm{av}: application version
  • tc,tut_c, t_u: creation and update timestamps

Tree Reconstruction

Given traces {τi}\{\tau_i\} with the same cycle_id\mathrm{cycle\_id}, we reconstruct the DAG:

Theorem (Unique Reconstruction): The parent-child relationships encoded in parent_id\mathrm{parent\_id} 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 kk cycles can be viewed as a forest:

Fk=i=1kGi\mathcal{F}_k = \bigcup_{i=1}^k G_i

where each GiG_i is a tree rooted at the user-agent interaction node.

Path Analysis

For any node NN in cycle GiG_i, the depth d(N)d(N) is the length of the unique path from the root to NN:

d(N)={0if N is root1+d(parent(N))otherwised(N) = \begin{cases} 0 & \text{if } N \text{ is root} \\ 1 + d(\text{parent}(N)) & \text{otherwise} \end{cases}

Definition (Interaction Depth): The depth of a cycle is:

Δ(Gi)=maxNVid(N)\Delta(G_i) = \max_{N \in V_i} d(N)

This represents the maximum nesting level of tool/agent invocations within the cycle.

Query and Result Arrays

For mm total tool calls across all cycles, we maintain:

q=[q1,q2,,qm]andr=[r1,r2,,rm]\mathbf{q} = [q_1, q_2, \ldots, q_m] \quad \text{and} \quad \mathbf{r} = [r_1, r_2, \ldots, r_m]

These can be indexed by a mapping function ϕ:(i,j)l\phi: (i,j) \mapsto l where (i,j)(i,j) represents the jj-th tool call in cycle ii and ll is the global index.

Tensor Representation

For advanced analysis, we construct a 3-tensor:

TRk×μ×n\mathcal{T} \in \mathbb{R}^{k \times \mu \times n}

where:

  • kk = number of cycles
  • μ=maxiVi\mu = \max_i |V_i| = maximum nodes in any cycle
  • nn = number of available tools

Element Tijl=1\mathcal{T}_{ijl} = 1 if the jj-th node in cycle ii invokes tool TlT_l, and 00 otherwise.

Proposition: Complete Reconstruction

Proposition: The quadruple (h,q,S,r)(h, \mathbf{q}, S, \mathbf{r}) where:

  • hh is the chat history
  • q\mathbf{q} is the query array
  • SS is the selection matrix
  • r\mathbf{r} 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 (h,q,S,r)(h, \mathbf{q}, S, \mathbf{r}):

  1. hh provides user inputs and agent outputs for each cycle
  2. SS identifies which tools were called and in what order
  3. q\mathbf{q} provides the queries sent to each tool
  4. r\mathbf{r} 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

  1. Simplified Storage: Each node stores a complete interaction pair, reducing edge complexity
  2. Natural Hierarchy: Parent-child relationships directly model invocation patterns
  3. Efficient Queries: Tree structure enables fast traversal and analysis
  4. Version Tracking: Node-level version information supports evolution analysis
  5. 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.