---
title: "GenerateContentConfig in Depth: Temperature, Top-P, Top-K, and Seed"
date: "2026-11-25T08:24:00+07:00"
description: "What each sampling parameter actually does inside Gemini generation — temperature, top-p, top-k, and seed — how they interact, and how to set them per task in ADK."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/generatecontentconfig-temperature-top-p-top-k-seed.jpg"
alt: "Probability distribution chart illustrating temperature and top-p sampling controls"
tags: ['gemini', 'sampling', 'generatecontentconfig', 'google-adk', 'llm-tuning']
---

Every creative choice a language model makes runs through a sampling stage, and the parameters controlling that stage — `temperature`, `top_p`, `top_k`, `seed` — are the most changed and least understood knobs in the stack. Teams crank temperature because a post told them to, then can't explain why outputs got worse. The reality: these parameters reshape a probability distribution, and each one is right for a *task class*, not a *project*.

Here's what each does mechanically, how they interact, and how to set them deliberately through `GenerateContentConfig` in ADK.

## The sampling stage in one picture

At each token step the model produces logits → a probability distribution over the vocabulary. Sampling then:

1. **Temperature** reshapes the whole distribution (flatter or sharper).
2. **Top-K** keeps only the K most likely tokens (hard cutoff by rank).
3. **Top-P** keeps the smallest set of tokens whose cumulative probability reaches P (nucleus, adaptive cutoff).
4. **Seed** makes the random draws reproducible when supported.

Order of operations matters: temperature first changes the probabilities, then the cutoffs are applied to the *reshaped* distribution.

## Temperature: the sharpness dial

```python
config = GenerateContentConfig(temperature=0.2)
```

- **Low (0.0–0.4):** distribution stays peaked — the model strongly favors its top candidate. Deterministic-feeling, safe, repetitive if pushed too far (near 0 can produce loops or degenerate "always the same" outputs).
- **Mid (0.5–0.8):** a balanced spread. Good general-purpose range for conversational agents.
- **High (0.9–1.0+):** flattened distribution, more tail tokens become reachable — variety, surprise, stylistic range.

Rule of thumb: **extraction, classification, tool selection → low. Chat → middle. Brainstorming, varied copy → high.**

## Top-P (nucleus sampling): the adaptive cutoff

`top_p=0.9` means: keep adding the most likely tokens until their cumulative probability hits 90%, then cut. Its beauty is adaptability — in a confident context the nucleus might be 2 tokens; in an ambiguous one it might be 200. The tail (impossible continuations) is always excluded, so high temperature can't drag the model into nonsense.

Most teams should treat **top-p as the "safety rail for creativity"**: pair it with temperature rather than fighting it. `temperature=0.9, top_p=0.95` explores broadly *within plausible space*. Setting both very high is how assistants start saying strange things.

## Top-K: the fixed-rank cutoff

`top_k=40` keeps the 40 most likely tokens regardless of how flat or peaked the distribution is. It's blunt — in a confident context where the top token has 99% probability, restricting to 40 changes nothing; in a context where 50 tokens are all plausible, it clips the 41st arbitrarily.

**Use cases where top-k still earns its place:** very low-temperature generation where you want a hard, cheap constraint; matching behavior of older open-model pipelines (many open-source serving stacks default it on); pairing with open models served via [Ollama or vLLM](/blog/adk-ollama-local-models-litellm-integration), where top-k is a standard control.

For Gemini API calls, temperature and top-p are usually the two that matter.

## Seed: reproducibility with an asterisk

```python
config = GenerateContentConfig(seed=42)
```

A seed fixes the random draws so the same request can return the same output — invaluable for **evals and debugging**: a test that flakes because of sampling noise hides real regressions.

The asterisk: seeding is a *best-effort* determinism aid, not a guarantee. Server-side changes, model updates, and floating-point behavior can still shift results. Use seeds to stabilize *comparison runs* ("did my prompt change cause this?"), not to promise users identical answers forever.

## How they interact (and fight)

| Combination | Effect | Verdict |
|---|---|---|
| `temp=0.1` alone | near-greedy, stable | ✅ extraction, routing |
| `temp=0.7, top_p=0.95` | varied but plausible | ✅ general chat |
| `temp=1.0, top_p=1.0` | unrestricted tail | ⚠️ rarely what you want |
| `top_k=1` | greedy regardless of temp | ✅ cheap determinism |
| `temp=0` + seed | fully pinned | ✅ eval baselines |

Practical guidance: **change one knob at a time and measure.** If outputs are bad at default settings, the fix is far more often the prompt (missing constraints, vague role) than the sampler. Sampling parameters are a scalpel applied *after* the prompt expresses the task correctly.

## Setting them in ADK

Two paths, and an important constraint:

```python
from google.adk.agents import Agent
from google.genai.types import GenerateContentConfig

agent = Agent(
    model="gemini-flash-latest",
    name="extractor",
    instruction=EXTRACT_INSTRUCTION,
    tools=[parse_record],
    generate_content_config=GenerateContentConfig(
        temperature=0.1,      # extraction wants stability
        top_p=0.9,
        seed=42,              # keep eval runs comparable
    ),
)
```

**The ADK constraint worth knowing:** on `LlmAgent`, `generate_content_config` is for *sampling and generation behavior* — setting `tools`, `system_instruction`, `response_schema`, or `http_options.base_url` there raises a `ValueError`. Those belong to their own agent fields (they're not knobs; they're structure). Keep the config to what it is for: temperature, top-p, top-k, seed, `max_output_tokens`, safety settings, and similar generation parameters.

## A defaults table worth keeping

| Task class | temperature | top_p | seed |
|---|---|---|---|
| Extraction / classification | 0.0–0.2 | 0.9 | optional |
| Tool selection / routing | 0.0–0.2 | — | optional |
| General assistant chat | 0.6–0.8 | 0.95 | — |
| Creative copy / variants | 0.9–1.1 | 0.95–1.0 | — |
| Eval baselines | 0.0 | 0.9 | fixed |

Revisit quarterly — these are starting points to beat with your own evals, not commandments.

## Where to go next

Sampling shapes *how* tokens are drawn; budgets shape *how many* — see [max output tokens and truncation strategies](/blog/adk-max-output-tokens-truncation-strategies), [stop sequences](/blog/adk-stop-sequences-early-generation-termination), and [thinking budgets](/blog/adk-thinking-models-reasoning-budgets). For structuring what comes out, read [Pydantic `output_schema` for structured outputs](/blog/adk-pydantic-response-schema-structured-outputs).

## References

- [Google Gemini models for ADK agents](https://adk.dev/agents/models/google-gemini/)
- [Simple agents with `LlmAgent`](https://adk.dev/agents/llm-agents/)
- [googleapis/python-genai](https://github.com/googleapis/python-genai)
- [google/adk-python](https://github.com/google/adk-python)
