When working with ChatGPT, sometimes you might face issues with cut-off code. Don’t worry! 🤗
ChatGPT Code Length Limit
Based on my experiments, the ChatGPT code length limit for GPT-4 is roughly 70 lines. This depends on the programming language and complexity of the program. A more complex program in a less space-efficient programming language would be cut off earlier. The actual length limit is not in terms of the number of lines but the number of tokens (e.g., ~4000 tokens), whereas each token represents one word, roughly speaking.
Here’s a screenshot of the snake program that was cut off after 71 lines:
Write the game Snake in Python using two snakes!
Method 1: Use Simple Prompts to Encourage Continuation
The first method to fix this problem is by using simple prompts, such as
"keep going", or
"continue", to encourage ChatGPT to provide more content or code. By doing this, you’re giving direction and nudging the language model into picking up where it left off.
OpenAI masters communicate their intention clearly to improve the output quality. If you’re a master of prompt engineering, you can get much more out of ChatGPT and LLMs.
Often you’ll hear people say “X cannot be done with ChatGPT”. I’ve seen many cases where this statement only reveals the low skill level of the person stating it.
Method 2: Let ChatGPT Break Your Problem Into Smaller Subproblems
The cleanest way to solve the problem of cut-off code due to the length restrictions is to properly guide ChatGPT through the more complex program.
Step 1: Prompt the function names of the big problem
first create a series of function names to write the game of snake with two snakes in Python. Don't write the functions out yet!
Step 2: Prompt the main function to tie it all together
Now write the main() function
Step 3: Prompt each function separately until all functions are done
Now write the initialize_game() function
Now write the next function
Step 4: copy&paste all code snippets and run the code
You may also do some housekeeping, such as importing libraries properly. In my case, I copied it all in a file
code.py and ran the code using the IDLE editor:
Also, note that if the code doesn’t work initially, keep feeding the error into ChatGPT until the error goes away. ChatGPT is far more powerful when used in “iterative mode” — much like we humans don’t get it right at first most of the time.
✅ Iterate with ChatGPT on different parts of your code until it works!
Method 3: Increasing Maximum Response Length
💡 A simple solution to tackle the ChatGPT code length limit issue is to adjust the maximum number of tokens generated by the model. By increasing the response length, you can prevent the output from being cut off abruptly.
🛠 To achieve this, you can use the
max_tokens parameter provided by the API. However, be careful while setting a lower value, as it might result in incomplete or confusing output 😕. Keep in mind that the token limit for
gpt-3.5-turbo is 4,096, and for the
gpt-4 model, it’s 8,192 (source).
Method 4: A Few Words on Token Limits for In- and Outputs
💡 In this method, you’ll learn to reduce the input and output tokens to prevent ChatGPT from generating cut-off code. By understanding the token limits, you can optimize your interactions with AI chatbots like ChatGPT and GPT-4.
🔖 ChatGPT has a token limit of approximately 4096 for input and output combined.
To avoid truncated code, follow these steps:
- 💻 Keep the input message brief: Summarize your initial request or question, so the AI has enough tokens left for a more complete response.
- 🤖 Check for ongoing code: If possible, review the response tokens to see if the AI has generated incomplete or cut-off pieces of code. Using this information, you can streamline your follow-up requests.
- ⚙️ Adjust the output tokens: You can use the API to change the number of output tokens to a smaller value (as shown in the previous step), making sure the response doesn’t exceed the ChatGPT token limits.
✅ Recommended: The Evolution of Large Language Models (LLMs): Insights from GPT-4 and Beyond
Method 4: Use the Latest GPT Version and Address Hallucinations
In this method, you’ll focus on debugging hallucinations and using the latest version to overcome ChatGPT code length limitations: 👇
Your code should be within the token limit for your chosen GPT model. For example,
gpt-3.5-turbo has a limit of 4,096 tokens, while
gpt-4 has a limit of 8,192 tokens.
Staying within this limit helps prevent code truncation or premature cut-off. However, if you still encounter issues with the cut-off code, follow these steps:
- Identify any hallucinations, those inaccurate or irrelevant code pieces, within the ChatGPT response. This may take some manual debugging to pinpoint the exact problematic areas 👀.
- Address these hallucinations and errors by providing clearer instruction or context in your prompts to ChatGPT. The closer you are to the model’s token limit, the higher the chance of a cut-off response.
- Consider using Python to create functions or scripts that break your desired output into smaller chunks (see above), making it easier for ChatGPT to generate complete code within the token limit 🐍.
- Utilize external tools or APIs that allow you to process batched text, increasing your capability to process and generate longer code segments.
✅ Recommended: Hallucinations in AI – with ChatGPT Examples
Method 5: Add a Prompt to Write Ultra-Concise (Python) Code
ChatGPT and GPT Playground have a token limit for input and output. You can tell it to write more space-efficient code at the cost of being less readable. If you love Python One-Liners like me, you’ll use this method even though it’s not recommended! 😉
Write the game Snake in Python using two snakes! Use very concise code and minimal symbols for variables, etc.
Finally, if you love this content, make sure to subscribe to the Finxter newsletter with plenty of ChatGPT and AI content — we even have some prompting cheat sheets and free tech tutorials you don’t want to miss.
✅ Recommended: Free ChatGPT Prompting Cheat Sheet (PDF)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.