Hit a strict character limit when AI shortens your text
A form field allows 500 characters with spaces. AI's own count of a shortened draft can be off; here is a prompt, a len() check and a feedback round to fix it.
The form field says "500 characters including spaces", and the draft is 1,198 characters. Before pasting whatever "shorten this to 500 characters" returns, count it.
The draft used here is a made-up personal statement for a data analysis course (an office worker who automated a monthly Excel inventory sheet): 1,198 characters with spaces, 980 without.
The short prompt
Shorten the text below to 500 characters. {draft}Two runs. Both added a line in parentheses after the text. The last lines of the two answers:
(About 470 characters.)(About 495 characters including spaces.)Saved to files and counted with Python, the answers look like this:
$ python3 check_len.py naive1.out.txt 500with spaces 497 | without spaces 409 | limit 500 -> ok$ python3 check_len.py naive2.out.txt 500with spaces 537 | without spaces 445 | limit 500 -> 37 over$ python3 check_len.py naive2.body.txt 500with spaces 495 | without spaces 409 | limit 500 -> okThe model's own numbers were close (the second answer's text is 495 characters, exactly what it said). What broke the limit is the extra line: the second file is 537 characters because the note came along. "Output only the text" has to be part of the prompt. Shortening to 300 and to 150 characters gave the same pattern: the extras pushed the saved file over (167 characters against a limit of 150). Across all six English runs (limits 500, 300 and 150, two each) the number in the parenthesis matched len() on the text itself exactly in five (495, 294, 149, 280, 143) and was off by 2 in one ("About 470" for a 472-character text).
A parallel test with a Korean draft behaved differently. In six Korean runs (limits 500, 250 and 300 without spaces) none of the printed numbers matched len() exactly: one answer that ended "(250 characters)" was 252, and another labeled "about 460" was 463. Models read text as tokens, not characters, so a printed count is an estimate, and with Korean it was a poorer one in these runs. Six runs per language is a small sample. Whatever the language, count the saved text yourself.
A prompt that fixes how to cut
Google's prompting guide says to state constraints on the response, such as length and scope, explicitly (prompting strategies). Saying what to cut is part of the constraint: whole sentences, no new facts.
Shorten the personal statement in <draft> to fit an application form field. Rules:- The result must be at most {limit} characters including spaces.- Shorten by deleting whole sentences or merging two sentences into one. Do not change facts (numbers, periods, job duties) in the sentences you keep.- Do not add anything that is not in the draft.- Output only the shortened text, with no character count and no commentary. <draft>{draft}</draft>The result at a limit of 500:
For three years I have worked in the back office of a logistics company, building the monthly inventory sheet in Excel. In the evenings I learned Python basics from online lessons and wrote a small script that cut the month-end report from six hours to two. Studying alone has limits, though. This program works on real regional data in team projects, and I would like to turn a problem from my job into an analysis task, get feedback from other learners, and have an instructor review my code.$ python3 check_len.py loop1.out.txt 500with spaces 494 | without spaces 407 | limit 500 -> okCount outside the model
#!/usr/bin/env python3"""usage: check_len.py text.txt limit - count with and without spaces, exit 1 if over"""import re, syst = open(sys.argv[1], encoding="utf-8").read().strip()limit = int(sys.argv[2])n, m = len(t), len(re.sub(r"\s", "", t))print(f"with spaces {n} | without spaces {m} | limit {limit} -> " + ("ok" if n <= limit else f"{n - limit} over"))sys.exit(0 if n <= limit else 1)Save only the text to be pasted into the form, not the whole chat answer. Forms differ in how they count line breaks and special characters, so paste the final text into the real field as well.
If it is over: give the number back
Google's guide for writing prompts recommends refining the result with follow-up prompts instead of expecting one perfect answer (writing effective prompts). Make the follow-up concrete: the measured length, the excess and the previous answer.
<previous> is the text you just shortened. Counted with Python len(), it is {length} characters including spaces, which is {excess} over the {limit}-character limit. Cut at least {excess} more characters so it is at most {limit} characters including spaces.- Delete or merge whole sentences only. Keep the sentences you leave unchanged.- Output only the shortened text. <previous>{previous}</previous>In the Korean test, the 252-character answer went back with "252 characters, 2 over", and the next answer was 235 characters, under the 250 limit. One round was enough; if it is still over, repeat with the new numbers two or three times.
Where it goes wrong
- The cut can be lopsided. In a run with a 300-character limit the result kept the opening sentence, the Python sentence and "Studying alone has limits, though.", and dropped everything about what the course would be used for. The count was right and the statement was thinner. Name the sentences that must stay.
- The answer changes from run to run. Text that lands at 495 of 500 can cross the limit on the next run. Aim a few percent under.
- Facts. Check that numbers, periods and job duties are the same as in the draft.
Do not paste personal details (names, addresses, phone numbers) into a prompt just to shorten them. Remove them first.