The initial question is to use the API, not ChatGPT.
The AI model won’t be able to accurately count characters on its own without excessive prompting and generation, like ‘…then, produce a JSON list in format [{“index”: 0, “character_position_letter”: “A”}, {…}], where each letter of SEO title is reproduced individually and the increasing index then shows the length obtained, to then automatically produce a new candidate title if this analysis shows the string is too long.’
Using code interpreter could be an “assistants” option, but requires the AI to write even more multi-turn scripts to use a python tool. $0.03 per session.
The solution I show needs no tools or excess generation, it simply gives the “try again” error with length information if the AI were to produce output too long.