Sub topic 6
Understanding the session lifecycle
This chapter assumes you know basic REST and HTTP methods, can read JSON, and understand what an OAuth 2.0 access token is. It also assumes you have an Agentforce agent built in your org. Here you learn how an Agent API conversation is structured before you write the calls in the next chapters.
What a session is
A session is one conversation between your external system and one agent. The agent keeps the conversation history and any variables inside the session. Every message you send belongs to a session, so the agent can answer a follow-up like "and the second one?" using what was said earlier.
A session belongs to one agent. If you need to talk to a different agent, create a different session.
The three stages
Every integration follows the same three stages.
- Create. You send a POST to the agent's sessions endpoint. The response contains a
sessionId. Salesforce also returns the agent's opening message. - Exchange messages. You send messages to the session, using the
sessionIdin the URL. Each reply comes back either as a single response or as a stream. - End. You send a DELETE to the session. Salesforce then releases the conversation and stops accepting messages for it.
What identifies a session
Two values matter, and they do different jobs.
| Value | Created by | Purpose |
|---|---|---|
| `externalSessionKey` | Your system | A unique key you choose, such as a UUID. It lets you correlate the session with your own records. |
| `sessionId` | Salesforce | The identifier you put in the URL of every later call. |
Keep the sessionId for as long as the conversation lasts. If you lose it, you cannot continue or end that session.
Ordering messages with sequenceId
Each message you send carries a sequenceId. It is an integer that you increment for every message in the session: 1 for the first, 2 for the second, and so on. The streaming example later in this tutorial uses sequenceId 2 because it is the second message in an existing session. Reusing a value or sending values out of order is a common source of errors, so keep the counter next to the sessionId.
Why you must end sessions
A session does not close because your script stopped. Until it is ended or times out, it stays open on the Salesforce side. Abandoned sessions waste resources and make usage harder to audit. End a session as soon as the conversation is finished.
The reliable pattern is to end the session in a finally block, so the DELETE call runs even when a message request fails. The complete example appears in the chapter on creating and using sessions. In outline, it looks like this:
session_id = create_session()
try:
send_messages(session_id)
finally:
end_session(session_id)The end call includes the header x-session-end-reason. A value such as UserRequest records why the session closed.
A session is not a login
The access token and the session are separate things, and they expire independently. The token proves who your application is. The session holds the conversation. A token can expire while a session is still open, and a new token does not start a new conversation. If a token expires mid-conversation, you request a new token and keep using the same sessionId. Handling this is covered in the chapter on errors and retries.
Common mistakes
- Creating a new session for every message. The agent then has no memory of earlier turns.
- Sharing one session between unrelated users. Their conversations and variables mix.
- Not storing the
sessionId, which makes it impossible to end the session later. - Skipping the end call when an exception occurs.
Exercise
A script sends three messages to an agent, then crashes before it ends the session. Which stage of the lifecycle was skipped, and what code structure prevents this?
Show answer
The end stage (the DELETE call) was skipped. Putting the DELETE call in a finally block makes it run even when an earlier call raises an error.
Exercise
Your access token expires halfway through a conversation. Do you need to create a new session?
Show answer
No. The token and the session are separate. Request a new token and keep sending messages to the same sessionId.