Skip to main content

LatentPort

LatentPort makes LLM internal states programmable.

Created by Simon P. Villani.

Capture a model's working context, transfer it to another model, compact its attention memory, or save and branch it for later use. Learned transformations operate on inference state so supported models can continue without replaying the historical context.

The interface follows the operation. Select a supported bridge or model when loading; use the same methods to work with its state.

transferred = port.bridge.translate(state)
compacted = state.compact()
branches = state.fork(2)
state.save("saved-state")

Translation and compaction each have their own compatibility requirements. The runtime checks them before proceeding; a shared interface does not make every model pair or sequence of operations compatible.

LatentPort: programmable model state

Website | Get started | Supported transformations | Cookbook | API

License: Apache-2.0 PyPI

State operations

Operation API Purpose
Capture model.capture(context) Read context and retain its complete inference state.
Transfer bridge.translate(state) Transform a source snapshot into the supported target model's state.
Compact state.compact() Reduce attention KV while preserving recurrent memory, convolution state and logical positions.
Append model.append(state, text) Process new input from an existing state.
Fork state.fork(2) Create independent descendants of a shared immutable snapshot.
Save / load state.save(path) / lp.State.load(path) Persist and restore state without historical prompt prefill.

Learned optimisation, multi-parent state combination and automatic operation selection are on the roadmap. They are not implemented in this release.

Get started

Install the lightweight package with Python 3.11 or later:

python -m pip install latentport
latentport doctor

Inference requires CUDA PyTorch and the appropriate backend dependencies. See installation for NF4, BF16 and GGUF setup. GGUF also requires a compatible LatentPort native library. Prebuilt libraries target specific GPU and CUDA configurations; check the guide before downloading models.

Set LATENTPORT_HOME to a directory with enough space for model downloads and caches. The runtime resolves pinned transformation artifacts and compatible base models. Model weights and transformation weights are separate downloads.

Transfer context

Choose bridge_repo from the supported transformations below. This workflow is the same for every supported bridge:

import latentport as lp

history = [("user", "The ticket is red.")]

with lp.load(bridge_repo, backend="nf4") as port:
    state = port.source.capture(history)
    transferred = port.bridge.translate(state)
    answer = port.target.session(transferred).ask("What color is the ticket?")
    print(answer.text)

The source reads the history, translate() creates target state, and the target processes the new question. Changing the bridge repository selects the model pair and trained transformation. It does not change the operation or require manual token slicing. port.handoff() remains an advanced interface for callers that already own an exact target-token suffix.

Use backend="nf4", "bf16" or "gguf" where the selected bridge supports it. Existing base files can be supplied with source_path and target_path. Thinking mode, transfer boundaries and tokenizer compatibility are properties of the selected checkpoint; see its model card and the API contracts.

Conversations

The composite session manages transfer and continuation with the same calls:

with lp.load(bridge_repo, backend="nf4") as port:
    chat = port.composite.session(history)
    answer = chat.ask("What color is the ticket?")
    print(answer.text, end="")
    while not answer.finished:
        answer = chat.generate()
        print(answer.text, end="")
    print()
    print(chat.ask("What evidence supports that answer?").text)

The default cached_handoff policy transfers the initial context once and keeps the target's state for follow-ups. The runtime selects the checkpoint's supported transfer boundary. Some bridges require the target to read the new question; others can transfer its prepared answer state. Neither route replays historical context on the target. A prefix-only bridge with no initial history runs the new question directly on the target because there is no context to transfer.

policy="strict" synchronizes completed answers back to the source and transfers again on subsequent turns. See the cookbook for streaming, answer continuation, branching and MTP requirements.

The same command accepts any supported bridge repository:

python examples/chat.py --repo BRIDGE_REPOSITORY --backend nf4 --context "The ticket is red." --prompt "What color is the ticket?"

Replace BRIDGE_REPOSITORY with a repository from the table below. Use --followup for another turn, or --backend gguf for a compatible native profile.

Compact context

Choose a supported model_id from the table below. The runtime selects the matching learned compaction weights from the state's identity:

with lp.load_model(model_id, backend="nf4") as model:
    state = model.prefill(history)
    compacted = state.compact()
    answer = model.session(compacted).ask("Continue the task.")
    print(answer.text)

The default policy retains one eighth of attention KV at or above 65,536 retained positions. Shorter histories return unchanged unless force=True is supplied. Use ratio=4 for a larger retained budget. Compaction preserves the logical clock and original positions; it does not replace context with a text summary.

For a running session, session.compact(reserve_tokens=...) can make room for an upcoming request within the configured physical capacity. Compaction reduces KV storage, not model-weight memory. See the compaction guide for context sizing, extended positions, backend options and validation receipts.

Reuse, branch and resume

review_state, planning_state = state.fork(2)
state.save("saved-state")
restored = lp.State.load("saved-state")

Prepare context once for several agents, retain a checkpoint before a risky step, or resume a saved session. Snapshots record exact model and runtime identities. Forks share immutable storage; advancing a branch requires its own working state. Snapshots contain prompt-derived tensors and conversation text, so protect them like the original conversation. See persistence and ownership.

Supported transformations

One Python runtime serves the following trained artifacts. This is the current support catalog, not a separate API for each checkpoint.

Operation Supported models Selection
Transfer Qwen3.5-4B to Qwen3.5-9B latentport/LPBridge-Qwen3.5-4b-Qwen3.5-9b as bridge_repo
Transfer Qwen3.5-4B to Qwen3.8-27B latentport/LPBridge-Qwen3.5-4b-Qwen3.8-27b as bridge_repo
Compact Qwen3.5-4B Qwen/Qwen3.5-4B as model_id; selector
Compact Qwen3.5-9B Qwen/Qwen3.5-9B as model_id; selector
Compact Qwen3.8-27B Qwen/Qwen3.8-27B as model_id; selector
Compact Qwen3.8-Flash-Next Qwen/Qwen3.8-Flash-Next as model_id; selector

Each bridge is directional and tied to exact checkpoints. Current bridges require dense source state with complete alignment metadata; compacted and suffix-only source states cannot be transferred. Supported quantizations and execution modes are declared per artifact. An unsupported combination fails explicitly.

Validation and performance

Published measurements describe specific checkpoints, hardware and workloads:

  • At 128K context, 8x compaction reduced attention KV from 4 GiB to 512 MiB on Qwen3.5-4B/9B and from 8 GiB to 1 GiB on Qwen3.8-27B. See compaction results and backend validation for exact configurations.
  • Historical resident transfer measurements reported 2.19x paired initial time-to-first-token speedup at approximately 16K tokens. This is not a general throughput claim or a benchmark for every bridge. See methodology and transfer results.

State copies, model loading, backend scheduling and retained branches affect memory and latency. GPU qualification and CPU contract tests cover different parts of the runtime; consult the records for the profile you intend to use.

Demos and documentation

Report reproducible issues at GitHub Issues. Include python -m latentport doctor, exact model identities and a minimal example with private prompts and credentials removed.

Project

Created by Simon P. Villani, LatentPort is an independent AI research project. It began with LatentPort: Beyond KV Cache, published in September 2026, and now includes learned transfer, compaction and state lifecycle operations. Learned optimisation, combination and automatic policy selection are the next stages of the project.

Original research code | Transformation models | Python package

License

The LatentPort runtime and transformation weights are Apache-2.0. Base models retain their upstream licenses, and llama.cpp retains its MIT license. See LICENSE and the notices supplied with model and native downloads.

Metadata

Release files for latentport 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for latentport 0.4.5
File Size Uploaded Provenance Actions
latentport-0.4.5.tar.gz 6.8 MB Without provenance Details for latentport-0.4.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for latentport 0.4.5
File Interpreter ABI Platform Actions
latentport-0.4.5-py3-none-any.whl Python 3 none any Details for latentport-0.4.5-py3-none-any.whl

Total release size: 7.8 MB

Release files / latentport-0.4.5.tar.gz

Download URL latentport-0.4.5.tar.gz
Size 6.8 MB
Tags Source
SHA-256 checksum
How to use checksums
e4914eb91ef24831072d41b9b9f1d787188a13ae5dd8452c0103e57bb9260a6f
BLAKE2b-256 checksum
How to use checksums
b0dede33eee99d507a54fbbfc93c4f24c1a8031854090facc9b6fb31510954ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / latentport-0.4.5-py3-none-any.whl

Download URL latentport-0.4.5-py3-none-any.whl
Size 982.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2db721760202402abe674678c78b0c279f164cc4bd0458b78b2ea2d32634edc1
BLAKE2b-256 checksum
How to use checksums
0979d45fa4cfd8e1e82db009aca44edf0ff56bdd7ed2b2ced1bb2603077eeb28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for latentport 0.4.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page