Profile, analyze, and optimize token consumption in LLM-powered applications and agentic workflows.
Project description
AgenticLens
AgenticLens is an open-source Python profiler for LLM applications and agentic workflows. It helps developers understand where tokens, latency, and cost are spent, then turns that profile into actionable budget optimization recommendations.
Think of it as a lightweight, local cProfile for AI workflows: no hosted
dashboard, no required backend, no account, and no data egress just to inspect a
run.
Why AgenticLens?
LLM applications rarely spend money in one place. Cost often leaks across planners, retrievers, memory, tool calls, repeated system prompts, and final response steps.
Most observability tools can show token usage. AgenticLens focuses on the next question:
What should I change to reduce the bill?
AgenticLens currently detects patterns such as:
- repeated system prompts that may be cached or deduplicated
- excessive retrieved chunks in RAG workflows
- low-utility retrieved chunks that appear unlikely to affect the final answer
- long conversation history that should be summarized or truncated
- duplicate tool calls that should be cached
- projected token, dollar-per-run, and monthly savings
Status
AgenticLens is early-stage software. The core profiling, cost calculation, export, CLI, and rule-based recommendation engine are implemented, but the API may still evolve before a stable 1.0 release.
Installation
For local development from this repository:
git clone https://github.com/agenticlens/agenticlens.git
cd agenticlens
uv sync --extra dev
If you do not use uv, install in editable mode with development extras:
python -m venv .venv
. .venv/bin/activate
pip install -e ".[dev]"
On Windows PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e ".[dev]"
Quickstart
Instrument your workflow with explicit profile() and step() blocks:
from agenticlens import profile, step
with profile("Customer Support Agent"):
with step(
"Planner",
type="planner",
provider="openai",
model="gpt-4o-mini",
prompt=planner_prompt,
) as s:
response = planner_llm.invoke(planner_prompt)
s.record(response)
with step(
"Retriever",
type="retriever",
chunk_count=12,
avg_tokens_per_chunk=80,
):
chunks = retriever.search(user_question)
with step(
"Final Answer",
type="final_response",
provider="openai",
model="gpt-4o-mini",
final_answer="Refunds are processed to the original payment method.",
) as s:
response = answer_llm.invoke(final_prompt)
s.record(response)
Then profile and analyze a script:
uv run agenticlens profile examples/recommendations_demo.py --save workflow.json
uv run agenticlens analyze workflow.json
Example output:
Budget Optimization Run cost: $0.0068; reducible: ~$0.0024/run (35%), ~$2.38/month.
Optimization Suggestions
* Long conversation history
* Excessive retrieved chunks
* Repeated system prompt
* Low-utility retrieved chunks
* Duplicate tool call
Estimated Savings: 35%
Core Concepts
Workflow
A workflow is one complete execution of an LLM application, such as answering a customer support question or running a multi-agent task.
with profile("Refund Support"):
...
Step
A step is a meaningful unit inside that workflow: planner, retriever, memory, tool call, LLM call, or final response.
with step("Retrieve Policy Chunks", type="retriever", chunk_count=10):
...
Recommendation
A recommendation is a rule-based optimization suggestion. Recommendations carry token savings, estimated percentage savings, dollar impact when pricing is known, confidence when relevant, and quality-risk notes for heuristics such as RAG chunk utility.
Features
| Area | Capability |
|---|---|
| Profiling | Explicit profile() and step() context managers |
| Metrics | Prompt tokens, completion tokens, total tokens, latency, TPS, cost |
| Providers | OpenAI and Anthropic response usage extraction |
| Costing | Local pricing table plus user pricing overrides |
| Recommendations | Repeated prompts, excessive chunks, low-utility chunks, long history, duplicate tool calls |
| Budget impact | Dollar-per-run and monthly savings projections |
| CLI | profile, report, and analyze commands |
| Export | JSON and CSV workflow export |
| Tooling | pytest, Ruff, mypy, GitHub Actions |
Cost Calculation
AgenticLens does not pull live prices from the internet. It uses a local pricing
table in src/agenticlens/config/pricing.yaml and this formula:
input_cost = (prompt_tokens / 1000) * input_price_per_1k
output_cost = (completion_tokens / 1000) * output_price_per_1k
total_cost = input_cost + output_cost
Pricing resolution order:
- User-supplied pricing override
- Bundled
pricing.yaml - Unknown model -> cost is reported as
None, not$0.00
This keeps reporting honest when model pricing is missing or stale.
RAG Chunk Utility
The RAG utility rule compares retrieved context with useful context. It can use explicit chunk metadata such as:
{"text": "...", "utility_score": 0.92}
{"text": "...", "used": True}
{"text": "...", "cited": False}
If explicit signals are unavailable, it falls back to lightweight word-overlap between retrieved chunks and the final answer. That fallback is intentionally conservative: it identifies potentially low-utility context, not chunks that are guaranteed safe to remove.
Examples
Run the recommendation demo:
uv run agenticlens profile examples/recommendations_demo.py --save workflow.json
uv run agenticlens analyze workflow.json
Other examples:
examples/basic_usage.pyexamples/rag_customer_support_demo.pyexamples/multiagent_support_demo.py
Some examples call real provider APIs and require provider API keys.
CLI Reference
Profile a Python script:
uv run agenticlens profile app.py
Save a workflow report:
uv run agenticlens profile app.py --save workflow.json
Display a saved workflow:
uv run agenticlens report workflow.json
Analyze a saved workflow:
uv run agenticlens analyze workflow.json
Development
Install development dependencies:
uv sync --extra dev
Run the test suite:
uv run pytest
Run linting, formatting, and type checks:
uv run ruff check .
uv run ruff format .
uv run mypy
Useful targeted checks while working:
uv run ruff check src tests
uv run ruff format --check src tests
Project Structure
src/agenticlens/
profiler/ workflow and step profiling
metrics/ cost and performance calculation
providers/ provider response usage extraction
recommenders/ rule-based optimization suggestions
exporters/ JSON and CSV exports
cli/ Typer CLI and Rich rendering
config/ pricing and settings
models/ Pydantic data models
Roadmap
Near-term priorities:
- richer RAG utility scoring with citation, reranker, and embedding signals
- model-tier mismatch detection
- prompt caching opportunity detection
- integrations for LangChain, LangGraph, LiteLLM, and OpenAI Agents SDK
- OpenTelemetry and OpenInference trace import
- optional prompt compression handoff
See ROADMAP.md and AgenticLens_Spec.md for more detail.
Contributing
Contributions are welcome. Good first areas include:
- provider integrations
- recommender rules
- example workflows
- docs and tutorials
- export formats
- test coverage
Please read CONTRIBUTING.md before opening a pull request.
Security
Please report vulnerabilities privately. See SECURITY.md.
Code of Conduct
This project follows CODE_OF_CONDUCT.md.
License
AgenticLens is released under the MIT License. See LICENSE.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agenticlens-0.1.2.tar.gz.
File metadata
- Download URL: agenticlens-0.1.2.tar.gz
- Upload date:
- Size: 133.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfc9a7cc372e405fd435cab3463cacf35bf138c31b7ef7ad8266f6ab0f8a1af5
|
|
| MD5 |
3468cf023b4d84777b2af9e25c57578a
|
|
| BLAKE2b-256 |
d26a930245806af63f9154b3c3f5e59e3373f80053c5f30269d0b076c6d2432c
|
File details
Details for the file agenticlens-0.1.2-py3-none-any.whl.
File metadata
- Download URL: agenticlens-0.1.2-py3-none-any.whl
- Upload date:
- Size: 31.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
456cc534d840727a013144b0afb70d596c113e801528646cc355918109a04a6b
|
|
| MD5 |
0beb33e9413f6ee38bca87e2785fc719
|
|
| BLAKE2b-256 |
bb836634fd16dd7db19d6f89f3daa1a97a0a1798ce33b52af5584d8ad5b1ebf2
|