1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
25 small wins to finish your pathNext lesson →
Keeping a long conversation short
A session only grows. Every turn is sent to the model on every later turn, so a long conversation gets slow and expensive before it gets confusing.
print("after three turns:", len(await session.get_items()), "items")
removed = await session.pop_item()
print("removed the last one:", removed["role"] if isinstance(removed, dict) else removed)
print("now:", len(await session.get_items()), "items")
await session.clear_session()
print("after clearing:", len(await session.get_items()), "items")Taking the last one back
removed = await session.pop_item()pop_item removes the most recent item and hands it to you. The obvious use is an undo button: the customer sends something by mistake, you take it off the end and the model never sees it again.
Starting over
await session.clear_session()Everything, gone. Useful when a conversation is finished, or when a customer asks you to forget what they said, which is a request you will eventually have to honour.
The three ways to keep it short
| Approach | Costs you | Use when |
|---|---|---|
| Let it grow | more tokens every turn | short conversations, and only those |
| Pop the old items | the early turns are gone | the beginning stopped mattering |
| Summarise, then clear | one model call to write the summary | long conversations where the early turns still matter |
The SDK gives you the first two. The third is yours to write: read the items, ask an agent to sum them up, clear the session, and put the summary back as the first item.
It fails at the worst time
This is not a problem you notice in testing. It arrives as a context length error in production, on your longest running conversation, which is usually your most important customer.
Try it yourself
- Pop twice and print what comes back each time.
- Write a loop that keeps only the last four items.
- Clear the session and run one more turn to confirm it starts fresh.
Every expert started right here.