JupiteX Get the app
Science & Technology30 Nov 2026 · about 8 min

One failed HTTP request gave me a 1,251-token Python traceback. My own code starts at token 865.

The brief

A traceback is the report Python prints when a program crashes. It shows the path of calls that were active when the error happened, along with the exception message. This matters because the report can be much larger than the part that points to your own code. Developers often send tracebacks to language models, and extra text can increase input-token use without necessarily adding useful clues. Each entry labeled with a file and line number is a frame. It represents one function call that was in progress. A library frame is a frame inside code installed separately or included with Python. The article’s compact format keeps frames from the app, the line that raised the error, and the full messages of chained exceptions. In the author’s five examples, that format reduced 2,553 tokens to 965, a 62% cut. But library frames can sometimes contain the evidence, especially when a bug is in a library or the app misuses a framework. The practical approach is to send a compact version first while keeping the full traceback in logs for follow-up.

01

What is a Python traceback, and what does a frame in it show?

A traceback is the report Python prints when a program crashes. It shows the path of calls that were active when the error happened, along with the exception message. This matters because the report can be much larger than the part that points to your own code. Developers often send tracebacks to language models, and extra text can increase input-token use without necessarily adding useful clues.

Each entry labeled with a file and line number is a frame. It represents one function call that was in progress. A library frame is a frame inside code installed separately or included with Python. The article’s compact format keeps frames from the app, the line that raised the error, and the full messages of chained exceptions.

In the author’s five examples, that format reduced 2,553 tokens to 965, a 62% cut. But library frames can sometimes contain the evidence, especially when a bug is in a library or the app misuses a framework. The practical approach is to send a compact version first while keeping the full traceback in logs for follow-up.

02

In the failed HTTP request, where does the app’s code first appear in the full traceback?

In the failed HTTP request, the app’s code first appears at token 865 of a 1,251-token full traceback. That means much of the report arrives before the model sees the first frame from the app. For an error sent to a model, where the useful code appears can matter as much as how much text is sent.

The request went to a closed port and produced a chain of four exceptions. Python prints chained exceptions oldest first. The traceback therefore opens with internals from urllib3, a library used in the request path, before reaching the app’s frame. This ordering explains why a traceback can begin with code the developer did not write.

The article warns that fixed-length cuts can remove the useful section. The first 3,000 characters of this traceback contained none of the app’s code; the last 1,000 characters also missed it. The last 2,000 characters happened to contain all five app frames. A compact format that selects frames is less dependent on where a text cut falls, while the full traceback remains available in logs.

03

How much shorter were the five tracebacks in the compact format, and how much did that reduce the example API costs?

The compact format cut the combined size of five tracebacks from 2,553 tokens to 965, a 62% reduction. That can make automated error reports smaller, but token savings do not translate directly into the same percentage saved on an API bill. The article’s cost example includes both input sent to the model and output produced by it.

For an equal mix of the five errors, the estimated monthly cost at 10,000 errors a day fell from $614 in full format to $423 in compact format. That is about a 31% reduction, not 62%. In the separate connection-error example, one call cost $0.0035 in full and $0.0019 in short. The estimates assume a 32-token instruction and an 80-token answer.

The reason is that the answer does not shrink when the traceback does, and output tokens cost more in the example pricing. On the named model, input cost $2 per million tokens and output cost $12. For a cheaper model, the same error mix cost $28 versus $19, so the savings mattered less in dollars. A coding agent may benefit more over repeated steps because earlier messages are sent again.

04

What can happen when an app cuts a traceback to a fixed number of characters before sending it to a model?

Cutting a traceback to a fixed number of characters can leave out the very code that would help explain the failure. If the model receives library internals and an error message but no frame from the app, it has less evidence to work with and may guess at the cause. The risk is not simply that the text is shorter; it is that the wrong part survives.

The article’s closed-port HTTP request shows how unevenly useful details can be placed. The first 3,000 characters contained none of the app’s code. The last 1,000 characters also contained none, because they were filled with requests internals and the final error message. A cut at the last 2,000 characters happened to include all five of the app’s frames.

That variation means a fixed-length rule cannot reliably preserve useful frames across errors. The proposed alternative is to build a compact traceback from the exception object, selecting app frames and the line that raised the error. Keep the full traceback in logs too: if the compact version is not enough, the complete report can be sent for further investigation.

05

How can a program keep its own useful traceback frames without simply cutting the error text?

Instead of shortening traceback text by character count, a program can inspect the exception object and select useful parts. The article’s function walks through the exception chain, extracts each traceback’s frames, and keeps frames whose filenames begin with the project folder. It also excludes site-packages, which matters when a virtual environment is inside the project.

If the last frame is not already among the selected app frames, the function adds it as the line that raised the error. It includes the full message for each exception, then formats the selected frame with a relative filename, line number, function name, and source line. It follows the cause or unsuppressed context to handle chained exceptions. This produces a compact report without relying on a blind text cut.

The function prints the newest exception first, unlike Python’s usual traceback order, so app frames appear near the top. Call it inside an except block. The compact version is not a replacement for full diagnostics: library frames can hold the answer when a library bug or framework misuse is involved. Keep writing the full traceback to a log, ready to send if needed.

06

What are chained exceptions, and why can a traceback show library errors before the app’s error?

A chained exception happens when a new error is raised while another error is being handled. Python prints the exceptions one after another, rather than showing only the newest one. That gives a fuller account of what happened, but it can also make the traceback longer and put the app’s code far from the start.

The closed-port request produced a chain of four exceptions. Python printed the oldest first, so the traceback began with urllib3 internals. The app’s first frame did not appear until token 865 in a 1,251-token traceback. In other words, a model reading from the beginning would encounter substantial library detail before reaching the app’s own call.

This ordering matters when an app cuts traceback text to a fixed size: the cut may exclude the app’s frames and leave the model with incomplete evidence. The article’s compact function instead walks through the exception chain and formats the newest exception first, bringing app frames near the top. It still keeps each exception’s full message and recommends retaining the full traceback in logs, since library frames can sometimes reveal the real problem.

07

What is a token in an AI model’s input, and why does reducing input tokens not reduce the total bill by the same percentage?

A token is a chunk of text, roughly three quarters of an English word. Model APIs bill for input tokens, which are sent to the model, and output tokens, which the model writes. This distinction matters when developers send tracebacks for help: shrinking the traceback reduces input, but it does not automatically shrink the answer or erase its cost.

In the article’s example, an alert bot sends a 32-token instruction and receives an 80-token answer. For an equal mix of five errors at 10,000 per day, compact tracebacks cut the estimated monthly cost from $614 to $423. The traceback tokens fell 62%, but the total bill fell about 31%, because the answer remained 80 tokens and output was priced higher.

The example prices were $2 per million input tokens and $12 per million output tokens. On a cheaper model, the same mix cost $28 in full and $19 in short, making the dollar difference small. A coding agent has another reason to reduce input: it resends earlier messages on each step, so a traceback read early is paid for repeatedly. Token counts can also vary across vendors’ tokenizers.

This brief was written by AI from the original reporting and checked by other models. Names, figures and quotes come from the source; read it for full context.

Read more in the JupiteX app

Pulse is free. New stories every 4 hours, each one broken into the questions that explain it.

Or read more news on the web