Pasting a Python error into an LLM is one of the most common things developers do with it, and many apps now do it automatically: catch the exception, send the text to a model, post the likely cause to a chat channel. The text is called a traceback, and most of it usually isn't about your code. I measured how much of it you can leave out.
A few words first
A token is a chunk of text, roughly three quarters of an English word. APIs bill per million tokens: input is what you send, output is what the model writes. A traceback is the report Python prints when a program crashes. Each File "...", line N entry in it is a frame, meaning one function call that was in progress. A library frame is one inside code you installed or that ships with Python. Chained exceptions happen when one error is raised while another is being handled: Python prints all of them, one after another.
The setup
I wrote a four-file app and broke it in five ordinary ways: an HTTP request to a closed port (with the requests library), a malformed YAML file, a model reply that isn't valid JSON, a missing dictionary key, and data that fails a JSON Schema check. These are real tracebacks from Python 3.13.5, with the app's folder shown as /srv/app. I counted tokens with tiktoken, OpenAI's tokenizer library, using the o200k_base encoding.
The "short" version keeps three things: the frames from my own app, the one line that raised the error, and the full message of every exception in the chain.
| Error | Frames (library) | Full, tokens | Short, tokens | Saved |
|---|---|---|---|---|
| Connection refused | 24 (19) | 1,251 | 413 | 67% |
| Malformed YAML | 16 (11) | 636 | 185 | 71% |
| Not valid JSON | 8 (3) | 293 | 129 | 56% |
| Missing dict key | 5 (0) | 138 | 75 | 46% |
| JSON Schema failure | 6 (1) | 235 | 163 | 31% |
All five together are 2,553 tokens in full and 965 in short, a 62% cut. The missing key shrinks even with no library frames, because the short format drops the folder names, the words File and line, and the ^^^^ markers Python prints under the code. Turning those markers off with python -X no_debug_ranges saves 13% overall, but that also removes some of the surrounding source lines in multi-line calls.
Where your code ends up
The connection error is the interesting one. It's a chain of four exceptions, and Python prints the oldest first. So the traceback opens with the internals of urllib3, and the first frame from my app appears at token 865 of 1,251.
That matters if you limit the size by cutting the text. The first 3,000 characters of this traceback contain none of my code. The last 1,000 characters don't either, because they're filled with requests internals and the final error message. The last 2,000 characters happen to contain all five of my frames. A cut at a fixed length can easily keep the wrong part, and then the model guesses.
What it costs
Suppose an alert bot sends each error to gpt-5.6 with a 32-token instruction and gets an 80-token answer. List prices are $2 per million input tokens and $12 per million output, from the catalog snapshot of the LLM gateway I work on (file dated November 25, 2026). With the connection error, one call is $0.0035 in full and $0.0019 in short. At 10,000 errors a day, that's $1,058 against $555 a month. For an equal mix of my five errors it's $614 against $423.
Note that a 62% smaller traceback doesn't mean a 62% smaller bill: the mix saves 31%, because output is priced higher and doesn't shrink. On claude-haiku-5.5, the same mix is $28 against $19, so on a cheap model the money hardly matters. (I used the same token counts for every model. Other vendors' tokenizers will differ.)
A coding agent is a different case. It keeps earlier messages and sends them again on every step, so a traceback read early is paid for repeatedly. Over 20 steps the connection error adds up to 25,020 input tokens in full and 8,260 in short.
The fix
Build the short version from the exception object rather than cutting text:
import traceback
def short_traceback(exc, root):
out = []
while exc:
frames = traceback.extract_tb(exc.__traceback__)
keep = [f for f in frames if f.filename.startswith(root)
and "site-packages" not in f.filename]
if frames and frames[-1] not in keep:
keep.append(frames[-1]) # the line that raised
out.append("".join(traceback.format_exception_only(exc)).strip())
for f in keep:
name = f.filename.removeprefix(root + "/")
out.append(f" {name}:{f.lineno} {f.name}: {f.line}")
exc = exc.__cause__ or (
None if exc.__suppress_context__ else exc.__context__)
return "\n".join(out)
Here root is your project folder, with no slash at the end. The site-packages check matters if your virtual environment lives inside the project. Unlike Python, this prints the newest exception first, so your own frames come near the top. Call it in your except block, and keep writing the full traceback to your log.
What this doesn't solve
I counted tokens and didn't measure answer quality. Library frames are sometimes the evidence: if you misuse a framework, or the bug is inside the library, the dropped part may hold the answer. That's why the full version should stay in the log, ready to send when the short one isn't enough.
Five errors in a small app are an illustration. A web framework would add its own frames, while a deep app of your own would keep more of them. The function also ignores exception groups (several errors raised together), prints only one line of a multi-line call, and says nothing about the values of your variables.
Finally, shorter doesn't mean safe. Error messages can contain URLs, tokens and customer data, and I keep those messages in full. Remove secrets before any of it leaves your server.
Top comments (0)