# Saving Previous Conversations in GPT API

**URL:** <https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597>\
**Category:** API\
**Tags:** gpt-35-turbo, chatgpt, api, langchain, assistants-api\
**Created:** [January 31, 2024, 9:06am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597 "2024-01-31T09:06:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfikmajeed2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alfikmajeed2/32/272199_2.png) [@alfikmajeed2](https://community.openai.com/u/alfikmajeed2)\
**Post date:** [January 31, 2024, 9:06am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/1 "2024-01-31T09:06:59Z")

</div>

Hello OpenAI community,  
I’ve been using the GPT API using python and I’m interested in finding an efficient way to save previous conversations without incurring additional token costs.

Currently, I’m building a chatbot, just like chat gpt My concern is that saving entire conversations and appending it into the prompt would lead to increased token usage and subsequently higher costs.

I’ve considered this conversation history appending method, but I’m reaching out to the community for advice and best practices.

If anyone has experience or suggestions on how to handle this efficiently, I would greatly appreciate your insights.

Thank you in advance for your time and assistance!

---

<div class="post-metadata">

**Author:** ![merefield](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/merefield/32/14812_2.png) [@merefield](https://community.openai.com/u/merefield)\
**Post date:** [January 31, 2024, 9:47am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/2 "2024-01-31T09:47:18Z")

</div>

Simply put, if possible you need to do that locally in your app.

---

<div class="post-metadata">

**Author:** ![alfikmajeed2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alfikmajeed2/32/272199_2.png) [@alfikmajeed2](https://community.openai.com/u/alfikmajeed2)\
**Post date:** [January 31, 2024, 9:59am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/3 "2024-01-31T09:59:13Z")

</div>

i didnt understand , could you explain breifly.?

---

<div class="post-metadata">

**Author:** ![merefield](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/merefield/32/14812_2.png) [@merefield](https://community.openai.com/u/merefield)\
**Post date:** [January 31, 2024, 10:09am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/4 "2024-01-31T10:09:09Z")

</div>

What platform is your chatbot being developed for? Does that have a database and the ability to store conversations? If so, use that.

---

<div class="post-metadata">

**Author:** ![alfikmajeed2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alfikmajeed2/32/272199_2.png) [@alfikmajeed2](https://community.openai.com/u/alfikmajeed2)\
**Post date:** [January 31, 2024, 11:37am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/5 "2024-01-31T11:37:14Z")

</div>

**yes, i am saving the conversations to mongodb database.**

---

<div class="post-metadata">

**Author:** ![alfikmajeed2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alfikmajeed2/32/272199_2.png) [@alfikmajeed2](https://community.openai.com/u/alfikmajeed2)\
**Post date:** [January 31, 2024, 11:38am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/6 "2024-01-31T11:38:32Z")

</div>

but,when i get this all chats from the db and give it with prompt. it cost more tokens(if the conversations is big).how to overcome this?

---

<div class="post-metadata">

**Author:** ![merefield](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/merefield/32/14812_2.png) [@merefield](https://community.openai.com/u/merefield)\
**Post date:** [January 31, 2024, 2:50pm UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/7 "2024-01-31T14:50:21Z")

</div>

you can limit what you send using a “look-behind” setting. It’s a compromise. You can increase this and send more history, or shorten it to decrease cost (and potentially increase the quality of response, paradoxically, because the prompt is shorter).

---

<div class="post-metadata">

**Author:** ![alfikmajeed2](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alfikmajeed2/32/272199_2.png) [@alfikmajeed2](https://community.openai.com/u/alfikmajeed2)\
**Post date:** [February 1, 2024, 4:12am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/8 "2024-02-01T04:12:02Z")

</div>

regex, lookbehind?. i think it will summarize the context right?

---

<div class="post-metadata">

**Author:** ![sarthak.srivastava](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sarthak.srivastava/32/19987_2.png) [@sarthak.srivastava](https://community.openai.com/u/sarthak.srivastava)\
**Post date:** [February 22, 2024, 6:45am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/9 "2024-02-22T06:45:22Z")

</div>

but if we sumaarize the previous chat history of user , than it is also adding cost for summarization for the chat histry

suppose we give all the histroy to openai api or langchain , it is incresing cost for the open ai api

is there any way that i have a raw histroy , and convert it into summarization whithout adding extra cost , and append that summarization as a prompt

---

<div class="post-metadata">

**Author:** ![icdev2dev](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/icdev2dev/32/148444_2.png) [@icdev2dev](https://community.openai.com/u/icdev2dev)\
**Post date:** [February 22, 2024, 7:35am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/10 "2024-02-22T07:35:43Z")

</div>

Yes. With betaassi, this is what you can do.

Class HistoricalMessage(BaseMessage):  
actual\_role:str = Field(default=“”)

Class HistoricalThread  
‘’‘Has HistoricalMessage’‘’

The key idea is to use OpenAi’s thread-message INSTEAD of using external storage.

Since OpenAi won’t let you insert an role other than “user” on the message, you use another field called actual\_role on the HistoricalMessage to copy from original thread.

So you have the complete history of the orginal thread.

Summarization is easy. Don’t use Assistant; use chat completion api with messages from historical thread to summarize. Remember to translate actual\_role to role (as chat completion allows different roles in messages as opposed to Messages in beta) and store in a SummarizedThread.

Class SummarizedThread(BaseThread):  
summary:Optional [str]=Field(default=“”)

If your summary \> 512 characters, you can define a composite field spanning multiple actual fields.

> **[GitHub - icdev2dev/betaassi: "betaassi: An innovative Python package for...](https://github.com/icdev2dev/betaassi)**
>
> "betaassi: An innovative Python package for seamless integration and management of OpenAI sessions, designed to enhance developer experience with asynchronous server support and extendable cla...

---

<div class="post-metadata">

**Author:** ![icdev2dev](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/icdev2dev/32/148444_2.png) [@icdev2dev](https://community.openai.com/u/icdev2dev)\
**Post date:** [February 22, 2024, 7:42am UTC](https://community.openai.com/t/saving-previous-conversations-in-gpt-api/608597/11 "2024-02-22T07:42:30Z")

</div>

Just save the conversations within OpenAI. See my response above to @sarthak.srivastava
