Skip to main content

Compress LLM context by rendering text as optimized images

Project description

PixelPrompt

Compress LLM context by rendering text as optimized images. Based on the research paper "Pixels Beat Tokens" (Venturi, 2026).

Why PixelPrompt?

When working with LLMs, token counts directly impact cost and latency. PixelPrompt converts text content into visually optimized PNG images, achieving 38-80% net cost savings compared to raw text tokens, with 100% accuracy on a 125-question benchmark (Claude Opus 4.6).

Key benefits:

  • 💰 38-80% net cost savings — input token reduction minus output cost, with optimized prompts
  • 🎯 100% accuracy — verified across prose, code, JSON, and config content types
  • 📊 Content-type presets — optimal settings for prose, code, JSON, and config files
  • 🔄 Automatic splitting — large content automatically split across multiple images
  • 📝 Prompt optimization — built-in helpers to eliminate output token inflation
  • 🚀 Easy integration — simple API for any Claude workflow

Installation

pip install pixelprompt

Quick Start

from pixelprompt import PixelPrompt, RenderConfig

# Use content-type presets for optimal settings
pxl = PixelPrompt(RenderConfig.for_content("json"))
images = pxl.render(json_data)

# Or use default settings (good for prose)
pxl = PixelPrompt()
images = pxl.render("Your long context here...")

# Use with Claude API
from anthropic import Anthropic

client = Anthropic()
message = client.messages.create(
    model="claude-opus-4-6-20250219",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                *[img.to_content_block() for img in images],
                {
                    "type": "text",
                    "text": "What are the key points? Answer with ONLY the answer value. No explanation, no preamble."
                }
            ]
        }
    ]
)

Content-Type Presets

Different content types have different optimal rendering settings. Use presets for best results:

from pixelprompt import RenderConfig, CONTENT_PRESETS

# Prose: font 9, minify=True — 69% net savings
config = RenderConfig.for_content("prose")

# JSON: font 9, minify=True (compact JSON) — 80% net savings
config = RenderConfig.for_content("json")

# Code: font 7, minify=False — 59% net savings
config = RenderConfig.for_content("code")

# Config (YAML, TOML, INI): font 8, minify=False — 39% net savings
config = RenderConfig.for_content("config")
Content Type Font Size Minify Input Savings Net Savings
JSON 9 Yes 83% 80%
Prose 9 Yes 71% 69%
Code 7 No 61% 59%
Config 8 No 41% 39%

Prompt Optimization

Critical: Without prompt optimization, image-mode responses are 2-4x more verbose, eating into your savings. Use the built-in helpers:

from pixelprompt import optimize_prompt, image_query

# Wrap any prompt with output-suppression
prompt = optimize_prompt("What is the server port?")
# → "What is the server port? Answer with ONLY the answer value. No explanation, no preamble."

# Build a complete image-query prompt
prompt = image_query("What functions are defined?", style="concise")
# → "Based on the content shown in the image(s): What functions are defined? Answer with ONLY the answer value. No explanation, no preamble."

Available styles: "concise" (default), "extract", "structured", "none".

Cost Comparison

Compare text vs image costs before committing:

pxl = PixelPrompt(RenderConfig.for_content("json"))
result = pxl.compare(json_data, model="claude-opus-4-6")

print(f"Text tokens: {result['text_tokens']}")
print(f"Image tokens: {result['image_tokens']}")
print(f"Input savings: {result['input_savings_pct']}%")

Configuration

from pixelprompt import PixelPrompt, RenderConfig

config = RenderConfig(
    font_size=9,           # Range: 6-20, default: 9
    font_family="monospace",  # "monospace", "serif", "sans-serif"
    max_width=1568,        # Max image width (Claude vision max: 1568)
    max_height=1568,       # Max image height (Claude vision max: 1568)
    dynamic_width=True,    # Fit width to content (saves tokens)
    dynamic_height=True,   # Fit height to content (saves tokens)
    minify=True,           # Strip markdown formatting for density
    content_type=None,     # Or use RenderConfig.for_content()
    padding=5,             # Minimal padding for density
    line_spacing=1,        # Tight line spacing
)

pxl = PixelPrompt(config=config)

API Reference

PixelPrompt

class PixelPrompt:
    def __init__(self, config: RenderConfig | None = None): ...
    def render(self, text: str) -> list[RenderedImage]: ...
    def compare(self, text: str, model: str = "claude-opus-4-6") -> dict: ...

    @staticmethod
    def minify_text(text: str) -> str: ...

    @staticmethod
    def compact_json(text: str) -> str: ...

RenderConfig

@dataclass
class RenderConfig:
    font_size: int = 9
    font_family: str = "monospace"
    max_width: int = 1568
    max_height: int = 1568
    dynamic_width: bool = True
    dynamic_height: bool = True
    minify: bool = True
    content_type: str | None = None

    @staticmethod
    def for_content(content_type: str) -> RenderConfig: ...

RenderedImage

class RenderedImage:
    width: int          # Image width in pixels
    height: int         # Image height in pixels
    tokens: int         # Estimated Claude vision token cost
    size_bytes: int     # PNG file size in bytes

    def png_bytes(self) -> bytes: ...
    def base64(self) -> str: ...
    def to_content_block(self) -> dict: ...  # Anthropic API format
    def save(self, path: str) -> None: ...

Prompt Helpers

def optimize_prompt(prompt: str, style: str = "concise") -> str: ...
def image_query(question: str, *, style: str = "concise", context_instruction: str | None = None) -> str: ...

Performance

Net cost savings by content type (Opus 4.6, with optimized prompts):

Content Type Net Savings Notes
JSON/Structured 80% Compact JSON + high density
Long Prose 69% Minification + word wrap
Source Code 59% Smaller font, preserve formatting
Config Files 39% Moderate density

Rendering time: ~100-200ms per image on modern hardware.

License

MIT License — see LICENSE file for details.

Citation

If you use PixelPrompt in research, please cite:

@software{pixelprompt,
  author = {Venturi, Gabriele},
  title = {PixelPrompt: Compress LLM Context by Rendering Text as Images},
  year = {2026},
  publisher = {Sinaptik GmbH},
  url = {https://github.com/sinaptik-ai/pixelprompt}
}

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

pixelprompt-0.4.0.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

pixelprompt-0.4.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file pixelprompt-0.4.0.tar.gz.

File metadata

  • Download URL: pixelprompt-0.4.0.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pixelprompt-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a6e1eb723b8689b55552cc0333befc5b68b2a56a6b5e9ba9d4099d86808c1527
MD5 eba5d71e387cfba98ab96697e40f2e38
BLAKE2b-256 deef80c5a22867d6657a39fcc1d06633c431824daa0c82bcf3e2eacdab3e6d88

See more details on using hashes here.

Provenance

The following attestation bundles were made for pixelprompt-0.4.0.tar.gz:

Publisher: publish.yml on sinaptik-ai/pixelprompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pixelprompt-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pixelprompt-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pixelprompt-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d2f4e173f60498fc3168306323ab6d3570363aa4e1c54c8fa695fb5cc19c0f5
MD5 6fece5a43e4c311bd93f909a01c70c3b
BLAKE2b-256 e0211df2c7d79200f5682f4af6cad2fbc6d2ff6b067268dc04f7761f05da046d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pixelprompt-0.4.0-py3-none-any.whl:

Publisher: publish.yml on sinaptik-ai/pixelprompt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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