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"}}'