Multi-agent Latent Space Communication for LLMs
Project description
LatentMesh
Multi-agent Latent Space Communication for LLMs
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
About The Project
LatentMesh wires multiple LLM agents together in a LangGraph pipeline by strictly communicating via KV cache.
In standard architectures, when Agent B needs to read what Agent A computed, it must process exactly the same text history from scratch. With LatentMesh, Agent B can access the prior agent's KV cache states, cutting costs computation costs from $O(n^2)$ to $O(n)$. Our long term vision is to expand communication between any model regardless of differences in architecture.
Getting Started
Installation
Clone the repository and install in editable mode
git clone https://github.com/shayhacker/LatentMesh.git
cd LatentMesh
pip install -e .
Alternatively, install via PIP
not yet available
pip install latentmesh
Optional Dependencies:
- For persistent disk-backed caching (recommended for production):
pip install latentmesh[disk]
- For booting a ready-to-go FastAPI endpoint:
pip install latentmesh[server]
Usage
A basic multi-agent configuration building a Plan -> Reason -> Review text pipeline.
from langgraph.graph import StateGraph, START, END
from latentmesh import LatentLLM, LatentState
from latentmesh.primitives import PlanPrimitive, ReasonPrimitive, ReviewPrimitive
from latentmesh.persistent_cache import MemoryKVStore, GlobalPrefixCache
# 1. Provide the cache to skip re-encoding string prefixes
store = MemoryKVStore()
cache = GlobalPrefixCache(store)
# 2. Use a standard HF model
llm = LatentLLM("Qwen/Qwen3-0.6B", device="cuda", global_cache=cache)
# 3. Create discrete agent primitives
planner = PlanPrimitive(llm)
reasoner = ReasonPrimitive(llm)
reviewer = ReviewPrimitive(llm)
# 4. Connect via LangGraph
builder = StateGraph(LatentState)
builder.add_node("planner", planner)
builder.add_node("reasoner", reasoner)
builder.add_node("reviewer", reviewer)
builder.add_edge(START, "planner")
builder.add_edge("planner", "reasoner")
builder.add_edge("reasoner", "reviewer")
builder.add_edge("reviewer", END)
graph = builder.compile()
# 5. Run it forward sequentially
result = graph.invoke({
"messages": [{"role": "user", "content": "What is the cosine of 45 degrees?"}],
"tokens_so_far": 0,
})
print(result["latent"].text)
print(f"Total tokens generated: {result['tokens_so_far']}")
Examples
For more involved architectures like consensus routing and dynamic state inspection, check out examples/:
| Example | Description |
|---|---|
sequential.py |
Plan → Reason → Review zero-shot pipeline |
complex.py |
Fast consensus parallel-voting with VotingPrimitive |
hierarchical.py |
Dynamic routing based on model confidence scoring |
Roadmap
- Implement HuggingFace model backend wrapping
- Add global prefix matching via
pygtrie - Create Memory and Disk KV Stores
- Multi-GPU parallelization for
VotingPrimitive - Implement LRU/TTL eviction policies for the global prefix cache
- Publish v1.0.0 to PyPI
See the open issues for a full list of proposed features (and known issues).
License
Distributed under the MIT License. See LICENSE for more information.
Contact
- Yash Ranjith - yranjith@stanford.edu
- Hiroki Kimiwada - kimiwada@stanford.edu
- William Peng - wgpeng@stanford.edu
- Atharva Rao - arao2007@stanford.edu
Project Link: https://github.com/shayhacker/LatentMesh
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file latentmesh-0.5.3.tar.gz.
File metadata
- Download URL: latentmesh-0.5.3.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be7d0924ae7fa2c2fe371ed61de7097d1f06cfe002f15c218f291ea3cfc5ee63
|
|
| MD5 |
487450af539bfbbf3cbaea75c9da9627
|
|
| BLAKE2b-256 |
65376b915d7f639b72f4f40edc271475c277898370ee8d572a25ace06e619e40
|
Provenance
The following attestation bundles were made for latentmesh-0.5.3.tar.gz:
Publisher:
publish-to-pypi.yml on shayhacker/LatentMesh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latentmesh-0.5.3.tar.gz -
Subject digest:
be7d0924ae7fa2c2fe371ed61de7097d1f06cfe002f15c218f291ea3cfc5ee63 - Sigstore transparency entry: 1191887473
- Sigstore integration time:
-
Permalink:
shayhacker/LatentMesh@ed179a374f0408c24129a3990793495cb65be94b -
Branch / Tag:
refs/tags/v0.5.3 - Owner: https://github.com/shayhacker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ed179a374f0408c24129a3990793495cb65be94b -
Trigger Event:
push
-
Statement type:
File details
Details for the file latentmesh-0.5.3-py3-none-any.whl.
File metadata
- Download URL: latentmesh-0.5.3-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e381345f35d594bdf293c2bbbacb6075d5ba350b34edf3af8d5faad51b7352d0
|
|
| MD5 |
45e7f9f955ce69816fd0ebc818368ca7
|
|
| BLAKE2b-256 |
ebafcdeccd9783b81813b25c7b1e62adc7e2817183be9539e81b8b0f345642a0
|
Provenance
The following attestation bundles were made for latentmesh-0.5.3-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on shayhacker/LatentMesh
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latentmesh-0.5.3-py3-none-any.whl -
Subject digest:
e381345f35d594bdf293c2bbbacb6075d5ba350b34edf3af8d5faad51b7352d0 - Sigstore transparency entry: 1191887478
- Sigstore integration time:
-
Permalink:
shayhacker/LatentMesh@ed179a374f0408c24129a3990793495cb65be94b -
Branch / Tag:
refs/tags/v0.5.3 - Owner: https://github.com/shayhacker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@ed179a374f0408c24129a3990793495cb65be94b -
Trigger Event:
push
-
Statement type: