module PromptBudgetCheck

Overview

Estimate the token size of prompts from a CSV and compare each with a budget. The estimate is characters divided by a rough ratio that you set. Real tokenizers differ, so treat every number as an estimate.

Defined in:

prompt_budget_check.cr

Constant Summary

DEFAULT_CHARS_PER_TOKEN = 4.0
DEFAULT_LIMIT = 1000
DEFAULT_WARN_AT = 80.0
REQUIRED = ["prompt_id", "text"] of ::String
USAGE = "usage: prompt-budget-check prompts.csv [--chars-per-token 4] [--limit 1000] [--warn-at 80]\nCSV columns: prompt_id, text. The ratio, limit and warn share are set by you; the token count is an estimate. More: https://www.onegtmlab.dev/glossary/prompt-engineering"
VERSION = "1.0.0"

Class Method Summary

Class Method Detail

def self.analyze(text : String, settings : Settings = Settings.new) : Report #

Parse the CSV text and build the report. Raises ArgumentError on bad input. Row numbers count the header as row 1.


[View source]
def self.cli(args : Array(String), stdout : IO, stderr : IO) : Int32 #

Command line entry. Returns the exit code: 0 ok, 1 bad input or file, 2 usage.


[View source]
def self.estimate_tokens(characters : Int32, chars_per_token : Float64) : Int64 #

ceil(characters / chars_per_token), guarding against float noise.


[View source]
def self.num(v : Float64) : String #

[View source]
def self.parse_csv(text : String) : Array(Array(String)) #

Small CSV reader: comma separated, double quoted fields may hold commas, line breaks and "" for a quote mark. Returns rows of raw cell strings.


[View source]
def self.render(report : Report) : String #

[View source]
def self.status_for(tokens : Int64, settings : Settings) : String #

[View source]