Usage alert: Total tokens exceeded the threshold using Openai API [closed]

I used following script for generating content by the Openai API.

It uses gpt4o model for content generating. When I define 1000+ words article I get error undefined message is showing, but when I defined below 1000 words then article generates perfectly and does not show any errors.

Recently i checked gpt4o increase their token limit 4096 to 8090.

In this moment, such an error is unexpected. I helped from chatgpt to optimize my prompt.

Same error is showing frequently. How to fix this ?

if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ai-seo-content-nonce' ) ) {
    wp_send_json_error( [ 'message' => esc_html__( 'Nonce verification failed.', 'super-fast-blog-ai' ) ] );
}
if ( empty( $_POST['title'] ) ) {
    wp_send_json_error( [ 'message' => esc_html__( 'Title is required.', 'super-fast-blog-ai' ) ] );
}

// Sanitize and assign variables
$title       = sanitize_text_field( wp_unslash( $_POST['title'] ) );
$seokeyword  = isset( $_POST['seo_keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['seo_keyword'] ) ) : '';
$metadescrip = isset( $_POST['metades'] ) ? sanitize_text_field( wp_unslash( $_POST['metades'] ) ) : '';
$tlanguage   = isset( $_POST['tlanguage'] ) ? sanitize_text_field( wp_unslash( $_POST['tlanguage'] ) ) : '';
$twstyle     = isset( $_POST['twstyle'] ) ? sanitize_text_field( wp_unslash( $_POST['twstyle'] ) ) : '';
$writone     = isset( $_POST['writone'] ) ? sanitize_text_field( wp_unslash( $_POST['writone'] ) ) : '';
$countWord   = isset( $_POST['countWord'] ) ? sanitize_text_field( wp_unslash( $_POST['countWord'] ) ) : '';
$htaging     = isset( $_POST['htaging'] ) ? sanitize_text_field( wp_unslash( $_POST['htaging'] ) ) : '';
$numberh     = isset( $_POST['numberh'] ) ? sanitize_text_field( wp_unslash( $_POST['numberh'] ) ) : '';
$faqlist     = isset( $_POST['faqlist'] ) ? sanitize_text_field( wp_unslash( $_POST['faqlist'] ) ) : '';
$subheading  = isset( $_POST['subheading'] ) ? sanitize_text_field( wp_unslash( $_POST['subheading'] ) ) : '0';
$this->imageApi = isset( $_POST['imageAPi'] ) ? sanitize_text_field( wp_unslash( $_POST['imageAPi'] ) ) : '';

// If subheading is enabled, ensure htaging and numberh are provided.
if ( '1' === $subheading && ( empty( $htaging ) || empty( $numberh ) ) ) {
    wp_send_json_error( [ 'message' => esc_html__( 'Please provide both the header tag and the number of subheadings if subheading is enabled.', 'super-fast-blog-ai' ) ] );
}

global $wpdb;
try {
    $config = new OpenAIConfig( $this->apikey );
    $client = new OpenAIClient( $config );
  
    $user_word_count  = intval( $countWord );
    // Estimate tokens needed (adjust multiplier if needed)
    $estimated_tokens = intval( $user_word_count * 1.5 );

    // Build the prompt in one frame
    $prompt = [
        [
            'role'    => 'system',
            'content' => esc_html__( 'You are a helpful assistant that generates SEO-friendly content.', 'super-fast-blog-ai' )
        ],
        [
            'role'    => 'user',
            'content' => sprintf(
                esc_html__( 'Generate a detailed article on "%1$s" with at least %2$d words.', 'super-fast-blog-ai' ),
                esc_html( $title ),
                $user_word_count
            )
        ]
    ];

    // Consolidate tone, style, and language instructions if provided
    $settings = [];
    if ( ! empty( $writone ) ) { $settings[] = "tone: " . esc_html( $writone ); }
    if ( ! empty( $twstyle ) ) { $settings[] = "style: " . esc_html( $twstyle ); }
    if ( ! empty( $tlanguage ) ) { $settings[] = "language: " . esc_html( $tlanguage ); }
    if ( ! empty( $settings ) ) {
        $prompt[] = [
            'role'    => 'user',
            'content' => 'Use the following settings: ' . implode( ', ', $settings ) . '.'
        ];
    }

    if ( ! empty( $seokeyword ) ) {
        $prompt[] = [
            'role'    => 'user',
            'content' => 'Include the following SEO keywords: ' . esc_html( $seokeyword )
        ];
    }

    if ( '1' === $faqlist ) {
        $prompt[] = [
            'role'    => 'user',
            'content' => esc_html__( 'Add a FAQ section at the end of the article.', 'super-fast-blog-ai' )
        ];
    }

    if ( '1' === $subheading && ! empty( $htaging ) && ! empty( $numberh ) ) {
        $prompt[] = [
            'role'    => 'user',
            'content' => sprintf(
                esc_html__( 'Include %1$d subheadings using %2$s tags, ensuring each contains relevant keywords.', 'super-fast-blog-ai' ),
                intval( $numberh ),
                esc_html( $htaging )
            )
        ];
    }

    // Final formatting instructions
    $prompt[] = [
        'role'    => 'user',
        'content' => esc_html__( 'Use only <h1>, <h2>, <h3>, <p>, <ul>, <li>, <b>, and <i> HTML tags for formatting.', 'super-fast-blog-ai' )
    ];

    // Call the OpenAI client with the optimized prompt
    $response = $client->chat( $this->aimodel, $prompt, $estimated_tokens );
    $content  = $response['choices'][0]['message']['content'] ?? '';

    if ( ! empty( $content ) ) {
        $post_data = [
            'post_title'   => $title,
            'post_content' => wp_kses( $content, wp_kses_allowed_html( 'post' ) ),
            'post_status'  => 'draft',
            'post_author'  => get_current_user_id(),
            'post_date'    => current_time( 'mysql' ),
        ];

        $post_id = wp_insert_post( $post_data );
        $stringcount = mb_strlen( $content, 'UTF-8' );

        if ( $post_id ) {
            $this->otslf_update_seo_meta_generate( $post_id, $seokeyword, $metadescrip );
            wp_set_post_terms( $post_id, get_option( 'otslf_ot_taxonomy', [] ), 'category' );
            $this->otslf_set_featured_image( $post_id, $title );

            $table_name = $wpdb->prefix . 'slf_schedule_post_title_log';
            $data = [
                'title'     => $title,
                'status'    => 'sameday',
                'postid'    => $post_id,
                'charaters' => $stringcount,
                'modelused' => $this->aimodel,
                'indicat'   => 'yes',
                'log_time'  => current_time( 'mysql' ),
            ];
            $wpdb->insert( $table_name, $data ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery  

            wp_send_json_success( [ 'message' => esc_html__( 'Post published successfully.', 'super-fast-blog-ai' ), 'title' => $title ] );
        } else {
            wp_send_json_error( [ 'message' => esc_html__( 'Failed to insert post.', 'super-fast-blog-ai' ) ] );
        }
    } else {
        wp_send_json_error( [ 'message' => esc_html__( 'Generated content was empty.', 'super-fast-blog-ai' ) ] );
    }
} catch ( Exception $e ) {
    wp_send_json_error( [
        'message' => esc_html__( 'Error: Failed to generate content. Please try again later.', 'super-fast-blog-ai' ),
        'error'   => $e->getMessage()
    ] );
}