Assistants API - No responses after thread reaches 20 messages

Hello all —

I’m working with the Assistants API and I’m experiencing an issue with thread size.

My current implementation is a simple chat application.

I can message back and forth with the assistant just fine, but when the thread reaches 20 messages, I no longer receive responses, and my new user message are not added to the thread.

Any ideas?

Hi and welcome to the Developer Forum!

Can you post a code snippet of your API calling and message adding code.

Hi! Thanks for the reply!

Here is the addThreadMessage route handler:

import { NextResponse } from 'next/server';
import OpenAI from 'openai';

const openai = new OpenAI(process.env.OPENAI_API_KEY);

export async function POST(req) {
  try {
    const request = await req.json();
    const { message, threadId } = request;

    const addMessage = await openai.beta.threads.messages.create(threadId, {
      role: 'user',
      content: message,
    });

    return NextResponse.json({ addMessage });
  } catch (error) {
    console.error(error);
    return NextResponse.error(error);
  }
}

And the runAssistant route:

import { NextResponse } from 'next/server';
import OpenAI from 'openai';

const openai = new OpenAI(process.env.OPENAI_API_KEY);
const assistantId = process.env.OPENAI_ASSISTANT_ID;

export async function POST(req) {
  try {
    const request = await req.json();
    const { threadId } = request;

    const run = await openai.beta.threads.runs.create(threadId, {
      assistant_id: assistantId,
    });

    return NextResponse.json(run);
  } catch (error) {
    console.error(error);
    return NextResponse.error(error);
  }
}