How to generate text of a specific length (in words)

Hello! Could you please tell me how to generate text of a specific length (in words) using the GPT model?

1 Like

You should try to use the max token limit, you can also use OpenAI’s pre-training as specified below:


I’ve heard this kind of thing works, by putting the following at the end of your prompt: “Reply with exactly 25 words.”


I tried doing that, but unfortunately, it didn’t work(

For now, I’ve solved the issue this way: at the end of each paragraph, I ask to output the number of words in the paragraph. I’ve also added in the prompt that I’d be very disappointed if there are fewer words than specified in the request. It sounds strange, but it worked. I’ll be testing how reliably this prompt works.

1 Like

:laughing: if it works it works!

An exact amount of words is hard :thinking: I wonder if the best way would be to set N=5 and then use whatever fits. Shotgun approach.


You can prompt the model to count each word, for example like this:
W1: A
W2: happy
W3: dog

W150: end

As soon as you get this to work, you try 2, 3 words etc…

Then when the text is done you use a simple regex that Chat GPT can write for you to clean the text.



Models have propensity to bugging and misunderstanding the prompt when large amounts of data are given.

Adding a system message which informs that “getting outside the w word limit will be penalized” should be enough.

1 Like

Well, it still needs to finish the sentence.

This sentence exists only to meet the requirements for minimum amount of text per post.

1 Like

Yeah, but the model can’t count, or at least it’s prone to counting errors.

Yes, I think this is really the problem. It’s not like it can “plan” or strategize the sentence out to match the correct word length. I think this is a good idea that may make it more reliable though. Possibly.

It’s not like it can “plan” or strategize the sentence out to match the correct word length.

It’s possible with more crutches but then it’s mostly for prompt sports then for production

Can you show a reliable example? Prompt sports sounds fun

True. What about multi-agent reasoning?

There you go:

It’s quite challenging as one runs into a whole host of issues, like limited reply length, alignment issues etc… but it can work.
I believe this is close to a multi agent approach as well, but we do it all inside a single prompt.

My name for this technique is “role based prompting and incremental context building”.
Additionally, this did not work in early 2023. I believe the model has improved since then.


Nice! That is definitely some prompt sports. I tried it myself and it worked great. It does tend to kind of “cut-off” the last bit, but it’s exact and still a sentence!

“Mustard Brown is a travel blogger who enjoys sampling exotic foods. He resides in San Francisco and owns a pet chameleon named Slick. His favorite cuisine is Ethiopian. Mustard dreams.”

Mustard dreams :laughing:

( I did post a link but I almost always clear my conversations, it works great though )

Erstelle eine Bewerbung für edeka

Maybe my use case is a bit different, but I want GPT to stay within x words, and not give me results with unfinished sentences.

With gpt-3.5 I have good results when I:

  • put in the prompt that i need a maximum of (e.g.) 50 words
  • put the max_tokens to a number a bit higher than that, let’s say 75.

Now GPT gives me back full sentences.

The AI model has no idea what you set max_tokens to, and that setting doesn’t affect the language production. It is just a safety switch to cut off AI gone crazy.

1 Like