How do I tell Codex to stop at the end of a function or comment?

How can I tell Codex to stop after it has given the answer I want? Setting max length doesn’t really help.

For example, in this prompt I entered lines 1 - 5 and expected line 6 but then it went and added lines 7+ on it’s own, which eats up tokens and is not necessary for my question.

I tried setting Temperature to 0 with no luck. And max length won’t help because, while in this trivial example the answer is short, in other examples the output could be naturally long.

Example 2: I tell it to write me a function and it actually gives me the function I want but also 4 other unrelated functions.

Any tips on how I can get it to give me just one answer to my question and then stop? I know for GTP-3 I can use stop sequences but I’m not sure how I could structure a stop sequence for something like this.