Skip to main content

LangChain Runnable client for TokenCrush prompt optimization API (/v1/crush)

Project description

tokencrush-langchain (Python)

LangChain-compatible Python client for TokenCrush prompt optimization API.

  • TokenCrushClient: thin HTTP client for POST /v1/crush
  • RunnableTokenCrush: a Runnable that accepts a prompt and returns the optimization result

Install

pip install tokencrush-langchain

Usage

from tokencrush_langchain import TokenCrushClient, RunnableTokenCrush

client = TokenCrushClient(api_key="YOUR_API_KEY")
res = client.crush({"prompt": "Summarize the following document ..."})
print(res.optimized_prompt)

crush = RunnableTokenCrush(api_key="YOUR_API_KEY")
result = crush.invoke("Rewrite this prompt to be concise while preserving meaning ...")
print(result.optimized_prompt)

API Contract

POST /v1/crush with JSON body { "prompt": string } and Authorization: Bearer <api key>.

Successful response shape:

{
  "optimized_prompt": "Optimized version of your prompt",
  "input_tokens": 10,
  "output_tokens": 6,
  "percentage_reduction": 40
}

Notes:

  • Only prompts with more than 20 tokens are optimized.
  • If similarity is below 80%, the original prompt is returned.
  • API may temporarily disable optimization for keys with poor performance.

Options

  • base_url: override API base (default https://api.tokencrush.ai)
  • fallback_to_input (Runnable): if API fails, return original prompt with 0% reduction

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

tokencrush_langchain-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

tokencrush_langchain-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file tokencrush_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: tokencrush_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for tokencrush_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d48dfe825a7beff212e0117e0d50463f1f6ddc75ba9b973484b30ef9c2050a13
MD5 0079a30956e0bc7efc5ccd03822540eb
BLAKE2b-256 a63b88b3a0339bbe85fb533724c28c7ef64f0491b938ec67680f421a2b2494bd

See more details on using hashes here.

File details

Details for the file tokencrush_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tokencrush_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 779a3d8ee837a479b0a1fd99b29fe197c7f047624e85c843cb50946c1d88cad3
MD5 450fa3a92c234a6d9d7663fcdfef0600
BLAKE2b-256 2f0a2b16ac15b77e8563c241cce9709fa11fb41dc60c817a3e63d988230e6dc1

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