Help us shape the future of Go! Take the 2026 Go Developer Survey

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: 0

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