Skip to main content

Compression-powered Context infrastructure for Agentic Systems

Project description

layer-context

Python SDK for ContextLayer - Compression-powered Context infrastructure for Agentic Systems.

Installation

pip install layer-context

For compression features (context compression + prompt compression):

pip install layer-context[compression]

# Pre-download the embedding model (~80MB) to avoid first-call latency:
layer-context-download

Quick Start

Initialize

from layer_context import CL

cl = CL(api_key="cl_sk_your_key_here")

Create a Block

cl.create_block("system-prompt")

Get a Block Handle

block = cl.block("system-prompt")

Append Context

block.append("You are a helpful assistant.")
block.append("Always respond in markdown.")

print(block.get_context())
# Output: "You are a helpful assistant.\nAlways respond in markdown."

Update (Replace) Context

block.update("You are a coding assistant. Use Python examples.")

Delete Specific Content

block.delete("Use Python examples.")

print(block.get_context())
# Output: "You are a coding assistant."

Delete a Block

cl.delete_block("system-prompt")

Versioning

block.create_version()

block.append("V2 content", version=2)
print(block.get_context(version=2))

View History

entries = block.get_history()
for entry in entries:
    print(f"{entry['action']}: {entry['content'][:50]}...")

List All Blocks

blocks = cl.list_blocks()
for b in blocks:
    print(b['name'])

Context Compression

Compress your stored context to reduce token usage while preserving relevance to a given query. Uses sentence embeddings, TextRank, and TF-IDF — runs entirely on your machine.

block = cl.block("support_faq")
block.append("You are a customer support assistant for Acme Corp.")
block.append("Always greet the user warmly and ask how you can help.")
block.append("If the user asks about billing, explain our pricing tiers.")
block.append("We offer Basic ($9/mo), Pro ($29/mo), and Enterprise (custom).")
block.append("For password resets, direct users to settings > security.")
block.append("Our refund policy allows refunds within 30 days of purchase.")

compressed = block.get_compressed_context(
    query="How much does the Pro plan cost?",
    target_tokens=50,
    aggressive_pruning=True,
)

print(compressed)
# Original: ~80 tokens -> Compressed: ~30 tokens

Arguments

Argument Type Default Description
query str required Topic/question to optimize relevance for.
version int 1 Which block version to compress.
target_tokens int | None None Max token budget. Defaults to ~50% of original.
relevance_threshold float 0.3 Min cosine similarity to keep a sentence.
dedup_threshold float 0.85 Similarity above which sentences are deduplicated.
aggressive_pruning bool False Strip filler words and verbose phrases.

Convenience Shortcut

compressed = cl.get_compressed("support_faq", query="billing question")

Prompt Compression

Compress any prompt at token level before sending it to your LLM — reducing costs and improving accuracy.

prompt = """You are a helpful customer support assistant for Acme Corp.
Always greet the user warmly and ask how you can help them today.
If the user asks about billing, explain our pricing tiers in detail.
We offer Basic ($9/mo), Pro ($29/mo), and Enterprise (custom pricing).
For password resets, direct users to the settings > security page.
Our refund policy allows full refunds within 30 days of purchase."""

# Compress the prompt by 50%
compressed = cl.compress_prompt(prompt, rate=50)

Arguments

Argument Type Default Description
prompt str required The prompt text to compress.
rate float 40 Compression percentage (0-100). A rate of 40 removes ~40% of tokens.

Full Pipeline Example

# 1. Get compressed context
context = cl.block("support_faq").get_compressed_context(
    query="billing question",
    target_tokens=50,
)

# 2. Build prompt
prompt = f"You are a support agent. {context} Answer the user's question."

# 3. Compress the final prompt
compressed_prompt = cl.compress_prompt(prompt, rate=40)

# 4. Send to LLM - fewer tokens, lower cost, higher accuracy

License

MIT

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

layer_context-0.2.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

layer_context-0.2.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file layer_context-0.2.0.tar.gz.

File metadata

  • Download URL: layer_context-0.2.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for layer_context-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f914454b32486e8a573b9f2cf560a80f22f8467b5fdd58ee5724fab61309a4b3
MD5 3566d05e4fff2d476b4a6241ba39c7c2
BLAKE2b-256 a86f5dbc943975c3e3b297802d6756c5a33143c114cf9af8b8141ccf8ec5ab33

See more details on using hashes here.

File details

Details for the file layer_context-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: layer_context-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for layer_context-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66065dace2e5c3f2a09d978b05e7d947845d0b8c67d82a9420044391f357440a
MD5 43c16015e0ad0f73095caa05ecacce68
BLAKE2b-256 5641946a91d1e3082c7871ee5a41bdeda0a0ecb11816fc925830cfbab0385978

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