From Script to Tool: Handling Reality
errors, retries, cost
Part of: How to Build with AI: The Playbook
A script works once on your machine. A tool works every time, on inputs you didn't expect. Closing that gap is this lesson, and it's why every guide has a harden section. Three things that will go wrong 1. The network fails. Calls time out or return errors. Wrap them and retry. 2. The output is malformed. You asked for JSON, and the model wrapped it in chatty text or a code fence. Parse defensively. 3. It costs money. Every call spends tokens. Long inputs and long histories add up. A safe call This does two jobs at once. It retries on failure, and it pulls out the JSON object even when the model added text around it. You'll reuse this pattern constantly. Watching cost A rough rule: one token is about four characters. Before shipping, ask how big your input is and how often it runs. A tool that resends a huge document on every call is a tool with a big bill. Later projects trim, cache, and summarize to keep cost sane. For now, just know that length equals money. The mindset Assume the model will sometimes return garbage and the network will sometimes drop. A good AI product fails politely and recovers instead of falling over. Every try/except you add is the difference between a demo