Reducing Context Tokens in Assistant Threads

Good call. Looks like there’s an undocumented/beta API endpoint.

# delete a message
def delete_message_gpt(thread_id, message_id):
    # have to hit this one directly, as it's unpublished/beta
    url = f"https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}"
    headers = {
        "Authorization": f"Bearer {app.config['OPENAI_API_KEY']}",
        "Content-Type": "application/json",
        "OpenAI-Beta": "assistants=v1",
    }

    response = requests.delete(url, headers=headers)

    if response.status_code != 200:
        raise Exception(f"Failed to delete message: {response.status_code} {response.text}")

Worth noting, the ability to delete messages from a thread is a useful user feature in ChatGPT. So it makes sense to have an endpoint so you can delete user messages and retry, or delete assistant messages and do a new run.

2 Likes