Skip to main content

Three-tier LangGraph checkpoint saver: L1 SQLite -> L2 NATS KV -> L3 PostgreSQL

Project description

3tears-langgraph

Three-tier LangGraph checkpoint saver: L1 (SQLite) -> L2 (NATS KV) -> L3 (PostgreSQL).

L1 and L2 are optional cache layers that degrade gracefully on failure. L3 (PostgreSQL) is the source of truth, reached through the AsyncQueryExecutor protocol so the same saver serves trusted services (direct asyncpg pool) and sandboxed agents (NATS L3 proxy).

Installation

pip install 3tears-langgraph

Usage

from threetears.langgraph import (
    AsyncpgPoolAdapter,
    ThreeTierCheckpointSaver,
)

# Trusted service with direct asyncpg.Pool: wrap once
saver = ThreeTierCheckpointSaver(executor=AsyncpgPoolAdapter(pool))

# Sandboxed agent: NatsProxyL3Backend already implements
# AsyncQueryExecutor, pass it straight through
saver = ThreeTierCheckpointSaver(executor=nats_l3_backend)

graph = builder.compile(checkpointer=saver)

Middleware

The package ships platform-level AgentMiddleware for langchain.agents.create_agent — the framework-aligned successor to the old hand-rolled AgentNodeHook / ToolNodeHook protocols. Consumer-specific policy lives in each consumer as its own middleware; only the reusable platform seams live here:

  • PromptCachingMiddleware (wrap_model_call) — annotates a leading bare-string system message with Anthropic cache_control={"type": "ephemeral"} when the model supports it, then normalizes cache-hit/creation counters onto usage_metadata["cache_usage"]. Non-Anthropic adapters degrade silently to bare-string system messages.
  • ToolResultOffloadMiddleware (wrap_tool_call) — when a ToolResultOffloader is injected on config["configurable"] and a tool result exceeds offload_threshold_chars, stores the full content out-of-band and shows the model "<summary>\n\n[ctx:<handle>]" (the structured artifact is preserved). Opt-in: no offloader ⇒ byte-for-byte no-op.
  • ObjectCatalogMiddleware (wrap_tool_call) — when a tool returns an ObjectHandle in its result artifact and an ObjectCataloger is injected, persists a catalog record under the verified call identity. Soft-fail side-effect: a catalog error never breaks the tool result.
from langchain.agents import create_agent

from threetears.langgraph import (
    ObjectCatalogMiddleware,
    PromptCachingMiddleware,
    ToolResultOffloadMiddleware,
)

agent = create_agent(
    model=chat_anthropic,
    tools=tools,
    middleware=[
        PromptCachingMiddleware(),
        ToolResultOffloadMiddleware(),
        ObjectCatalogMiddleware(),
    ],
)
# after a run, PromptCachingMiddleware has stamped:
# message.usage_metadata["cache_usage"]
# == {"cache_read_input_tokens": ..., "cache_creation_input_tokens": ..., "cached_tokens": ...}

The offload / catalog contracts (ToolResultOffloader, ObjectCataloger) are pure structural Protocols exported from threetears.langgraph.offload / threetears.langgraph.catalog; a consumer injects a concrete implementation on config["configurable"] (e.g. tool_result_offloader, object_cataloger) without the package taking any dependency on the consumer's context store.

See 3tears/docs/prompt-caching.md for the full caching contract, summarization interaction, downstream wiring checklist, and a worked example.

Streaming

The package ships StreamingResponse, a transport-agnostic primitive that owns the lifecycle of one streaming response: start -> any number of emit_token / emit_tool_call_* -> mutually-exclusive end (success) or error (failure) terminal. run_graph(compiled_graph, state, config) consumes a LangGraph astream_events(version="v2") loop with the start/end ordering managed; on graph exception it fires error(code="AGENT_FAILED", ...) and re-raises so the caller still sees the failure on the synchronous path.

The wire vocabulary is fixed: StreamStartEvent / StreamTokenEvent / StreamEndEvent / StreamErrorEvent / ToolCallStartEvent / ToolCallEndEvent / ToolCallProgressEvent, dispatched via the StreamEvent discriminated union and the parse_stream_event(payload) adapter. The transport seam is the StreamTransport Protocol -- one method, async def publish(self, payload: bytes) -> None. Any wire (NATS subject, websocket, chunked HTTP body) satisfies it.

from threetears.langgraph import StreamingResponse, StreamTransport

class WebSocketStreamTransport:
    """example transport for a websocket consumer."""
    def __init__(self, ws): self._ws = ws
    async def publish(self, payload: bytes) -> None:
        await self._ws.send_bytes(payload)

stream = StreamingResponse(
    transport=WebSocketStreamTransport(ws),
    correlation_id=correlation_id,
    conversation_id=conversation_id,
    start_time_monotonic=request_start,
)
final_state = await stream.run_graph(compiled_graph, state, config)

A reference adapter can bind the primitive to a per-correlation-id stream subject via nc.publish_raw. Tool-call observation envelopes flow through ToolCallProgressHook reading the active StreamingResponse from config["configurable"]["streaming_response"].

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

3tears_langgraph-0.14.1.tar.gz (101.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

3tears_langgraph-0.14.1-py3-none-any.whl (75.2 kB view details)

Uploaded Python 3

File details

Details for the file 3tears_langgraph-0.14.1.tar.gz.

File metadata

  • Download URL: 3tears_langgraph-0.14.1.tar.gz
  • Upload date:
  • Size: 101.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for 3tears_langgraph-0.14.1.tar.gz
Algorithm Hash digest
SHA256 4a7287ebcd89b7771cc01c24f9b7a50731209667489f3f7b9400d80da5bff74e
MD5 886ceed3a09d48f3194369b1f9288b1e
BLAKE2b-256 c33bc579278fce973d351e901047eef251f30ea3a9bca85e72f4b3537ee326b3

See more details on using hashes here.

File details

Details for the file 3tears_langgraph-0.14.1-py3-none-any.whl.

File metadata

  • Download URL: 3tears_langgraph-0.14.1-py3-none-any.whl
  • Upload date:
  • Size: 75.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for 3tears_langgraph-0.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d25b01148df31f458870be557c63362405564078a435546aa959bc186d373862
MD5 06c8d00c75e0106905b819eaa88e3d0b
BLAKE2b-256 1eef1fe254348de66594395cb7de3a0480fa5067c9aadbdf9b3df91558492a4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page