Cheap, Clean, and Unbreakable
cost and defensive parsing
Part of: Tone Rewriter
Two things stand between a working demo and a tool you'd let other people use. The model's reply is messier than you hope, and every call costs money. This lesson hardens both. The model wraps the rewrite You asked for "only the rewritten text." The model, trying to be helpful, sometimes returns this instead: Display that raw and your user sees code fences and a chatty preamble wrapped around their rewrite. Clean it defensively: drop fence lines, strip a leading label, trim the blank edges: The rule is simple: never trust the reply to be exactly what you asked for. Strip the wrapper every time. It's cheap insurance against ugly output. Cost is length times frequency Every call spends tokens on the input (system prompt plus text) and the output (the rewrite). A rough rule of thumb is one token per four characters. Two habits keep the bill sane: - Cap the input. A user pasting a whole book shouldn't trigger one giant, expensive call. Set a ceiling and refuse (or chunk) anything past it. - Set max tokens sanely. A rewrite runs about the same length as the input, so a two-line email doesn't need max tokens=4000. Right-size it. Fail politely, not silently Networks drop and models occasi
Challenge: Scrub the Fenced Reply