# Issues with Assistant API + threads

**URL:** <https://community.openai.com/t/issues-with-assistant-api-threads/496041>\
**Category:** API\
**Tags:** api\
**Created:** [November 12, 2023, 12:39am UTC](https://community.openai.com/t/issues-with-assistant-api-threads/496041 "2023-11-12T00:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikkdev](https://avatars.discourse-cdn.com/v4/letter/n/ea666f/32.png) [@nikkdev](https://community.openai.com/u/nikkdev)\
**Post date:** [November 12, 2023, 12:39am UTC](https://community.openai.com/t/issues-with-assistant-api-threads/496041/1 "2023-11-12T00:39:05Z")

</div>

anything i’m missing? each time i send message seems like it’s creating a new thread id or not sticking into 1 thread id, for that reason it’s not remembering the previous messages or the whole conversation.

```auto
async function askGPT(question) {
    try {
        conversationHistory.push({"role": "user", "content": question});
        // Retrieve the Assistant
        const myAssistant = await openai.beta.assistants.retrieve(assistantId);
        console.log(myAssistant); // For debugging

        // Create a new thread for each conversation
        const threadResponse = await openai.beta.threads.create();

        // Add a message to the thread with the user's question
        await openai.beta.threads.messages.create(threadResponse.id ,{
            role: "user",
            content: question
        });

        // Run the assistant to get a response
        const run = await openai.beta.threads.runs.create(
            threadResponse.id,
            { assistant_id: assistantId }
          );
        
        let runStatus = await openai.beta.threads.runs.retrieve(
            threadResponse.id,
            run.id
          );

        // Polling for run completion
        while (runStatus.status !== 'completed') {
            await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
            runStatus = await openai.beta.threads.runs.retrieve(threadResponse.id, run.id);
        }

        // Retrieve the messages after the assistant run is complete
        const messagesResponse = await openai.beta.threads.messages.list(threadResponse.id);
        
        const aiMessages = messagesResponse.data.filter(msg => msg.role === 'assistant');

        // Assuming the last message is the assistant's response
        return aiMessages[aiMessages.length - 1].content[0].text.value;
    } catch (error) {
        console.error('Error in askGPT:', error.response ? error.response.data : error);
        return 'An error occurred while processing your request.'; // Placeholder response
    }
}

```

---

<div class="post-metadata">

**Author:** ![sandst1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sandst1/32/246708_2.png) [@sandst1](https://community.openai.com/u/sandst1)\
**Post date:** [November 13, 2023, 10:02am UTC](https://community.openai.com/t/issues-with-assistant-api-threads/496041/2 "2023-11-13T10:02:03Z")

</div>

If i understand the API correctly, there is no automatic thread management. So this line will always create a new thread every single time it’s called.

```auto
 // Create a new thread for each conversation
const threadResponse = await openai.beta.threads.create();

```

so each time you call `askGPT`, a new thread is created.

There does not seem to be any method/API for getting a list of created Threads, which means that you need to store the thread id on your side to a database.

So instead of always calling `create()`, you need to check if a thread id exists in your database, then fetch it with `openai.beta.threads.retrieve(thread_id)`. And if it doesn’t exist, then create a new thread.

---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 22, 2023, 9:34am UTC](https://community.openai.com/t/issues-with-assistant-api-threads/496041/3 "2023-12-22T09:34:08Z")

</div>



---

<div class="post-metadata">

**Author:** ![EricGT](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ericgt/32/20571_2.png) [@EricGT](https://community.openai.com/u/EricGT)\
**Post date:** [December 22, 2023, 9:34am UTC](https://community.openai.com/t/issues-with-assistant-api-threads/496041/4 "2023-12-22T09:34:45Z")

</div>

As this topic has a selected [solution](https://community.openai.com/t/issues-with-assistant-api-threads/496041/2), closed topic.
