Skip to main content

A lightweight utility library to flatten tool JSONs and semantically optimize LLM prompt histories.

Project description

CikkanOps (cikkan-ops) 📉

License Python

CikkanOps (derived from the Tamil word Cikkaṉam, meaning thriftiness/eliminating waste) is a lightweight, production-grade input optimization library for LLM pipelines.

Passing raw, deeply nested JSON objects or unmanaged chat histories directly to LLM context windows causes severe token bloat and escalates API costs. CikkanOps serves as a lightweight optimization middleware to intercept your inputs, flatten structured data, and isolate semantic context vectors using TF-IDF similarity math—saving up to 40%+ on input tokens without dropping vital details.


✨ Features

  • 📊 Data Gateway Compression: Dynamically flattens deeply nested tool/API outputs into space-efficient CSV string matrices.
  • 🧠 Prompt Gateway Optimization: Routes historical logs into distinct runtime context zones (Hot / Cold), keeping semantic historical context while filtering out chat noise.
  • 🏷️ Tiktoken Native Tracking: Precise byte-pair encoding (BPE) counts matching OpenAI GPT-3.5/GPT-4 specifications to provide accurate analytics on total cost reductions.

⚙️ Installation

Install the library directly from your GitHub repository using pip:

🚀 How To Use: Step-by-Step IntegrationTo optimize your costs, insert CikkanOps as an intermediary interceptor step directly before making calls to an LLM provider (e.g., OpenAI, Anthropic, or LangChain).

Step 1: Intercept Complex Tool JSON ResultsWhen a custom tool or external API returns a heavy JSON object, call compress_tool_output() to transform it into a compact format before appending it to your LLM system prompt context.

Step 2: Intercept Long Chat History BuffersRight before routing your active query to an LLM, pass your query along with your structural history array into optimize_prompt(). This pulls out semantic matches and reference tokens while purging text noise.

Complete Orchestration ExampleHere is a complete script demonstrating both optimization interception stages:Python

    import os
    from openai import OpenAI
    from cikkan_ops import AICostOptimizer

    # 1. Initialize the CikkanOps Client
    # top_k=2 specifies how many relevant semantic background records to retrieve
    optimizer = AICostOptimizer(top_k=2)

    # 2. Instantiate your standard LLM Client
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))


    print("--- STEP 1: INTERCEPTING BLOATED TOOL JSON ---")
    # Example response from a data mining tool or system database query
    raw_tool_json = {
        "status": "fetched",
        "metadata": {"cluster": "aws-east", "latency_ms": 140},
        "records": [
            {"id": "USR-8819", "profile": {"name": "Alice", "tier": "enterprise"}, "logs": ["login", "export"]},
            {"id": "USR-3021", "profile": {"name": "Bob", "tier": "standard"}, "logs": ["password_reset"]},
            {"id": "USR-4409", "profile": {"name": "Charlie", "tier": "premium"}, "logs": ["create_workspace"]}
        ]
    }

    # INTERCEPTION STEP: Convert messy multi-line JSON payload to tight CSV row streams
    optimized_tool_context = optimizer.compress_tool_output(raw_tool_json)

    print("Optimized Context Ready for LLM:")
    print(optimized_tool_context)


    print("\n--- STEP 2: INTERCEPTING & ROUTING PROMPT HISTORY ---")
    # Heavily padded developer operation history containing critical tokens mixed with noise
    chat_history = [
        "System cluster validation token established: SEC-X8839-KEY.", # <-- Regex Rule Match
        "Developer checked in raw production deployment scripts.",
        "Discussed setting up automated cron jobs for server backups.",
        "Team lead mentioned that we need to order pizza for the hackathon.", # <-- Structural Noise (Dropped)
        "Let's make sure we order at least 3 vegan options for the design team.", # <-- Structural Noise (Dropped)
        "Ran initial database migration schema successfully creating 14 tables.", # <-- Semantic Query Match
        "The weather index outside is quite warm and comfortable today.", # <-- Preserved via Hot Zone
        "User started modifying configuration setup specifications.",       # <-- Preserved via Hot Zone
        "Bot replied acknowledging incoming session updates."              # <-- Preserved via Hot Zone
    ]

    user_query = "What is the token assigned for the cluster validation check?"

    # INTERCEPTION STEP: Extract critical data indices and drop context noise
    optimization_payload = optimizer.optimize_prompt(prompt=user_query, chat_history=chat_history)

    # This returns a cleanly structured prompt framework string
    final_llm_input_string = optimization_payload["optimized_prompt"]


    print("\n--- STEP 3: DISPATCHING OPTIMIZED STRINGS TO LLM ---")
    # The final string is passed straight to the LLM completion API
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": f"You are a helpful systems assistant. Current data references:\n{optimized_tool_context}"},
            {"role": "user", "content": final_llm_input_string}
        ]
    )

    print(f"LLM Response: {response.choices[0].message.content}")


    print("\n--- STEP 4: VERIFY COST SAVINGS METRICS ---")
    metrics = optimizer.get_metrics()
    print(f"Total Raw Tokens Processed:  {metrics['total_tokens_processed']}")
    print(f"Total Input Tokens Saved:    {metrics['total_tokens_saved']}")
    print(f"Total Context Reduction:     {metrics['savings_percentage']}")

📊 Performance MatrixIn baseline evaluations involving large production data streams mixed with standard chat logs, CikkanOps delivers the following performance optimizations:

Metric Component,Unoptimized Input,CikkanOps Optimized,Token Savings (%) Tool Payload Parsing,312 Tokens (JSON),148 Tokens (CSV),~52.5% Context History Logs,130 Tokens (Padded),75 Tokens (Sifted),~42.3%

📜 License

Distributed under the Apache License 2.0. See LICENSE for more information.

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

cikkan-1.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

cikkan-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file cikkan-1.0.0.tar.gz.

File metadata

  • Download URL: cikkan-1.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for cikkan-1.0.0.tar.gz
Algorithm Hash digest
SHA256 617ff2c6b13116aa907aa4f8bb1722a7950062ab7e5717c3a82c20361eeddff5
MD5 ed22d19d40c8b4d049fca23b6e1ab211
BLAKE2b-256 25b7b6959ae794210ee0759caa6a7e89c175cfefb066f7f8f3edb00bfc904934

See more details on using hashes here.

File details

Details for the file cikkan-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cikkan-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for cikkan-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75d50931520402e4067c89734b9a76367c86b8ff5038aceed73ece2e27322f1c
MD5 e3150e9da9dedc541380c570c0f4db3b
BLAKE2b-256 fa6f0e55c376ac6f0783317aea3149f253be081eef99bba8e41d876c3b53da5a

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