context-compression

command
v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This sample demonstrates the experimental ContextCompression middleware on a research loop that fills the context with verbose tool output.

The model searches for reports and fetches each one in its own turn. The token budget is small on purpose, so compression starts partway through the loop: older report bodies are truncated, and once that is not enough, a smaller model summarizes the earlier turns. The model works from the compressed view, while the history the flow returns keeps every message in full, with the compression recorded in message metadata.

The flow reports what was compressed next to the answer: the stats the middleware puts on the response, how many messages the history holds, and how many of them the model received on its last call.

Run it (needs a Gemini API key in the environment):

go run .

Or with the Dev UI, to watch the summarizer call nest inside the loop at http://localhost:4000/traces:

curl -sL cli.genkit.dev | bash    # install the Genkit CLI, once
genkit start -- go run .

Or over HTTP:

curl -X POST 'http://localhost:8080/researchFlow' \
  -H "Content-Type: application/json" \
  -d '{"data": {"project": "Project Alpha"}}'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL