πŸ“ services/toolUseSummary/ Β· 04_payload_optimization.md

Chapter 4: Payload Optimization

πŸ“„ services/toolUseSummary/04_payload_optimization.md

Chapter 4: Payload Optimization

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.

The Motivation

Imagine your AI agent runs a tool called readFile on a massive document (like a 5,000-line log file).

  1. The Action: The agent reads the file.
  2. The Output: A string containing 100,000 characters of text.
  3. The Goal: We just want a summary saying "Read 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.

The Concept: truncateJson

To 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.

The Use Case

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)

Internal Implementation

How does the system actually do this? It's a simple pipeline.

The Workflow

sequenceDiagram participant App as Application participant Opt as Optimizer (truncateJson) participant API as AI API App->>Opt: Sends Huge Object (10MB) Opt->>Opt: 1. Convert Object to Text Opt->>Opt: 2. Measure Length Opt->>Opt: 3. Cut text at 300 chars Opt->>App: Returns Small String (300 bytes) App->>API: Sends Small String

Code Deep Dive

Let's look at the helper function truncateJson located in toolUseSummaryGenerator.ts. We will break it down into tiny steps.

Step 1: Converting to Text

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.

Step 2: The Logic Check

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.

Step 3: The "Scissor" Cut

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."

Step 4: Safety First

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.

Using the Optimizer

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.

Summary

In this chapter, we learned about Payload Optimization.

  1. The Problem: Large tool outputs can overwhelm the AI or cost too much.
  2. The Solution: Truncation (cutting the data).
  3. The Implementation: We convert data to text, check the length, and slice it if necessary.
  4. The Result: A reliable system that generates summaries even when processing 10MB log files.

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