Skip to main content

prompt-flamegraph

Lightweight, zero-dependency Python package to profile LLM prompt tokens with interactive flamegraphs, waste detection, prompt diffs and HTML/SVG/Markdown/terminal exports.

What it does

prompt-flamegraph takes a structured prompt (system prompt, tools, RAG context, chat history) and shows you where the tokens go, in an interactive flamegraph.

It is intentionally lightweight: no proxy, no server, no dashboard, no telemetry. One function call, one output file.

Interactive HTML flamegraph

Install

pip install prompt-flamegraph

Extras:

pip install prompt-flamegraph[tiktoken]   # accurate OpenAI-style token counts
pip install prompt-flamegraph[rich]       # prettier terminal output

Quick start

from prompt_flamegraph import profile_prompt

prompt = {
    "system_prompt": "You are a helpful coding assistant.",
    "tools": ["..."],
    "rag_context": {"doc_1": "..."},
    "chat_history": ["..."],
}

profile_prompt(prompt, output="context.html")

Open context.html in your browser.

CLI

# HTML flamegraph
prompt-flamegraph prompt.json -o context.html --cost 1.5e-6

# Terminal bar chart
prompt-flamegraph prompt.json --terminal

# Diff between two prompts (green = added, red = removed, orange = changed)
prompt-flamegraph v1.json --diff v2.json -o diff.html

# SVG or Markdown export
prompt-flamegraph prompt.json --format svg -o context.svg
prompt-flamegraph prompt.json --format md -o context.md

# Demo
prompt-flamegraph --demo --cost 1.5e-6

Terminal example

────────────────────────────── Prompt Flamegraph ──────────────────────────────
Total: 102 tokens
 Category         Tokens      %  Visual
 system_prompt        18  17.6%  ████
 tools                41  40.2%  ██████████
 rag_context          22  21.6%  █████
 chat_history         21  20.6%  █████

Features

  • Pure Python, no required dependencies.
  • Optional tiktoken support.
  • Pluggable tokenizer.
  • Cost estimation.
  • Token waste detection: duplicates, oversized RAG, long history, too many tools.
  • Prompt diff: compare two prompts and visualize token changes.
  • Terminal output: colored ASCII/Rich bar chart.
  • Export formats: HTML, SVG, Markdown.
  • Works with nested dict, list and str structures.

API

profile_prompt(data, output, title, tokenizer, cost_per_token, detect_waste, width, height)

Build and render a prompt flamegraph to HTML.

diff_prompts(v1, v2, output, title, ...)

Render a diff flamegraph between two prompts.

detect_waste(tree)

Analyze a tree and return a WasteReport with findings.

build_tree(data, name, tokenizer)

Build the internal token tree without rendering.

Source

https://github.com/fjjjuv/prompt-flamegraph

License

This project is licensed under the GNU General Public License v3.0 or later.

See the LICENSE file for details.

Download files

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

Source Distribution

prompt_flamegraph-0.2.1.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

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

prompt_flamegraph-0.2.1-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file prompt_flamegraph-0.2.1.tar.gz.

File metadata

  • Download URL: prompt_flamegraph-0.2.1.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for prompt_flamegraph-0.2.1.tar.gz
Algorithm Hash digest
SHA256 46b2cb546db7d0d16c0ee0170b08ded2796088bec1f1deed71d0af2ee3f1c7f0
MD5 a598d0ab4131316a3df651f80169ef58
BLAKE2b-256 314b19637223b963921aed73d0004a9dacba3a845f959aa0c2e4ad508dc2509b

See more details on using hashes here.

File details

Details for the file prompt_flamegraph-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for prompt_flamegraph-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 86ed70e542c274417938af8cfcd76dab0a9c8a05eb52c70cf573d3595cdfed0a
MD5 5407b84391076b1f9b3ea1b61b397f8f
BLAKE2b-256 440b1c73f25d8621811b544971f9afddafda92e50610ef7bbbcec3f75198c418

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.3

2 files

0.2.2

2 files

This release

0.2.1 This release

2 files

0.2.0

2 files

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