# What does the \`use\_cache\` in \`generate\` actually do?

**URL:** <https://discuss.huggingface.co/t/what-does-the-use-cache-in-generate-actually-do/38855>\
**Category:** 🤗Transformers\
**Created:** [May 6, 2023, 3:05am UTC](https://discuss.huggingface.co/t/what-does-the-use-cache-in-generate-actually-do/38855 "2023-05-06T03:05:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nasheed](https://avatars.discourse-cdn.com/v4/letter/n/ea666f/32.png) [@nasheed](https://discuss.huggingface.co/u/nasheed)\
**Post date:** [May 6, 2023, 3:05am UTC](https://discuss.huggingface.co/t/what-does-the-use-cache-in-generate-actually-do/38855/1 "2023-05-06T03:05:16Z")

</div>

This is an offshoot from [Why past\_key\_values is not in GreedySearchDecoderOnlyOutput? - 🤗Transformers - Hugging Face Forums](https://discuss.huggingface.co/t/why-past-key-values-is-not-in-greedysearchdecoderonlyoutput/23843)

What does the `use_cache` in `generate` actually do? Since there is no way to provide the `past_key_values` to the `generate` function, does that mean that generate internally stores `past_key_values` between runs (say I run `generate` in a loop for a chatbot)?

---

<div class="post-metadata">

**Author:** ![joaogante](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/joaogante/32/20106_2.png) [@joaogante](https://discuss.huggingface.co/u/joaogante)\
**Post date:** [May 9, 2024, 3:28pm UTC](https://discuss.huggingface.co/t/what-does-the-use-cache-in-generate-actually-do/38855/2 "2024-05-09T15:28:33Z")

</div>

@nasheed

You can find the answer [here](https://huggingface.co/docs/transformers/v4.40.2/en/llm_tutorial_optimization#321-multi-round-conversation) 🤗

TL;DR: pass `return_dict_in_generate=True` to `generate` → `generate` returns `past_key_values` → pass `past_key_values=past_key_values` to `generate`
