# ChatGPT Code Length Limit: 5 Easy Ways to Fix Cut-Off Code

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:

`π Prompt: `Write the game Snake in Python using two snakes!``

## Method 1: Use Simple Prompts to Encourage Continuation

`π©βπ» Prompt: `continue!``

The first method to fix this problem is by using simple prompts, such as `"go on"`, `"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

`π©βπ» Prompt: `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

`π©βπ» Prompt: `Now write the main() function``

### Step 3: Prompt each function separately until all functions are done

`π©βπ» Prompt: `Now write the initialize_game() function``
`π©βπ» Prompt: `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:

1. π» Keep the input message brief: Summarize your initial request or question, so the AI has enough tokens left for a more complete response.
2. π€ 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.
3. βοΈ 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.

## 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:

1. 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 π.
2. 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.
3. 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 π.
4. 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! π

`π Prompt: `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)