Welcome back! In the previous chapter, Prompt Configuration, we taught our AI "writer" exactly what style of summary we wanted (short, past tense, Git-commit style).
However, even the best writer will struggle if you dump a dictionary on their desk and say "Summarize this in 5 seconds."
In this chapter, we will tackle Payload Optimization. We will learn how to shrink massive amounts of technical data into a bite-sized "preview" so our AI can understand the context without being overwhelmed.
Imagine your AI agent runs a tool called readFile on a massive document (like a 5,000-line log file).
If we send all 100,000 characters to the AI just to get a 3-word summary, we create two problems:
We need a mechanism to create a "Movie Trailer" of the dataβjust enough to know what happened, but not the whole movie.
truncateJsonTo solve this, we use a concept called Truncation.
Think of it like a File Compressor or a Tweet limit. We set a strict character limit (e.g., 300 characters).
....This ensures that our "Payload" (the data packet we send to the API) is always optimized and safe.
Let's say our tool output looks like this:
{
"file_content": "Line 1: Start...\nLine 2: Loading...\n... [500 more lines] ...\nLine 500: Error."
}
We want to transform that huge object into a simple string that fits in our pocket.
Target Output:
{"file_content":"Line 1: Start...\nLine 2: Loa... (stops at 300 chars)
How does the system actually do this? It's a simple pipeline.
Let's look at the helper function truncateJson located in toolUseSummaryGenerator.ts. We will break it down into tiny steps.
First, we can't measure the length of a generic "Object." We need to turn it into a string (text). We use jsonStringify (a wrapper around JSON.stringify).
function truncateJson(value: unknown, maxLength: number): string {
try {
// Convert the complex object into a simple text string
const str = jsonStringify(value)
// ... next steps
Explanation:
We accept value (which could be anything) and maxLength (our limit). We attempt to turn that value into a string string using jsonStringify.
Now that we have a string, we simply check its length.
// Inside the function...
// If it fits, return it as-is!
if (str.length <= maxLength) {
return str
}
// ... truncation logic
Explanation:
If the data is small (like filename: "test.txt"), we don't need to change anything. We just return it.
If the data is too big, we cut it.
// If it's too long, slice it.
// We subtract 3 to make room for the dots "..."
return str.slice(0, maxLength - 3) + '...'
Explanation:
We use .slice(0, X) to take the characters from the start (0) to our limit. We add ... to indicate to the human or AI reading it that "there is more data here, but we hid it."
What if the data is broken or can't be turned into text (e.g., a circular reference in code)? We don't want our app to crash.
} catch {
// If anything goes wrong during conversion, return a safe placeholder.
return '[unable to serialize]'
}
}
Explanation:
We wrap the whole thing in a try/catch block. If jsonStringify explodes, we calmly return a placeholder string instead of crashing the program.
Now let's see how this is used in our main generator. This connects back to Tool Summary Generator.
// From file: toolUseSummaryGenerator.ts
const toolSummaries = tools.map(tool => {
// 1. Optimize the input
const inputStr = truncateJson(tool.input, 300)
// 2. Optimize the output
const outputStr = truncateJson(tool.output, 300)
// 3. Format into a readable block
return `Tool: ${tool.name}\nInput: ${inputStr}\nOutput: ${outputStr}`
})
Explanation:
We apply truncateJson to both the input and the output of every tool. We set the limit to 300 characters. This guarantees that no matter how crazy the tool's execution was, our summary request will always remain small, fast, and cheap.
In this chapter, we learned about Payload Optimization.
We have built a robust system! We have the structure, the generator, the prompt instructions, and the optimization.
But there is one final piece of the puzzle. We are dealing with networks and APIs. Sometimes, the internet breaks. Sometimes, the AI is offline.
In the final chapter, we will learn how to handle these failures gracefully so they don't stop the user's work.
Next Chapter: Non-Blocking Error Handling
Generated by Code IQ