# Stopping \`model.generate()\` based on custom token

**URL:** <https://discuss.huggingface.co/t/stopping-model-generate-based-on-custom-token/3456>\
**Category:** Intermediate\
**Created:** [February 1, 2021, 3:21am UTC](https://discuss.huggingface.co/t/stopping-model-generate-based-on-custom-token/3456 "2021-02-01T03:21:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jodiak](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@jodiak](https://discuss.huggingface.co/u/jodiak)\
**Post date:** [February 1, 2021, 3:21am UTC](https://discuss.huggingface.co/t/stopping-model-generate-based-on-custom-token/3456/1 "2021-02-01T03:21:40Z")

</div>

Hello everyone, I’ve managed to train a huggingface model that generates coherent sequences based on my training data and am using `generate` to create these new sequences. This has worked well enough so far however I need to stop sequence generation based on the count of a particular token that denotes the start of a subsequence in my domain. Is there a way to leverage the `generate()` method to do this? ie rather than generate based on length generate until n number of a particular token are generated.

---

<div class="post-metadata">

**Author:** ![jodiak](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@jodiak](https://discuss.huggingface.co/u/jodiak)\
**Post date:** [February 22, 2021, 7:28am UTC](https://discuss.huggingface.co/t/stopping-model-generate-based-on-custom-token/3456/2 "2021-02-22T07:28:50Z")

</div>

I found that the best way to do this is by directly calling the model with the necessary inputs rather than using the generate method, and to build logic around this that checks the number of a particular token in the resulting sequence and stops once its reached.

---

<div class="post-metadata">

**Author:** ![StackSmasher](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@StackSmasher](https://discuss.huggingface.co/u/StackSmasher)\
**Post date:** [October 18, 2021, 2:03pm UTC](https://discuss.huggingface.co/t/stopping-model-generate-based-on-custom-token/3456/3 "2021-10-18T14:03:29Z")

</div>

Can you share your code snippet for doing this ? I want to implement a similar custom generate function but can’t parse through the entire codebase in a short time
