Sub topic 12
Passing variables and context to the agent
This chapter assumes you can already create a session and send a message with the Agent API, as shown in the earlier chapters. It also assumes you know what a JSON array of objects looks like.
Why pass context
An agent often needs facts that only your system knows: who the customer is, which order page they are viewing, or which language they speak. You can put this in the user's message text, but the agent then has to extract it from prose. Variables give it structured values instead.
A variable is a named, typed value that you send with an API request. The agent reads it as part of its context.
The variable object
Each variable has three fields.
| Field | Meaning |
|---|---|
| `name` | The variable name the agent expects |
| `type` | The data type, for example `Text` |
| `value` | The value you are sending |
Names fall into two groups. Names that start with $Context. are standard context variables, such as $Context.EndUserLanguage. Names without that prefix are custom variables that you defined on the agent. A custom variable has to exist on the agent before you can send it. Check its exact name and type in Agentforce Builder.
Sending variables when you create the session
Send variables that stay the same for the whole conversation in the session request. Add a variables array next to the fields you already use.
import os
import uuid
import requests
API_BASE = "https://api.salesforce.com/einstein/ai-agent/v1"
MY_DOMAIN = os.environ["SF_MY_DOMAIN"]
AGENT_ID = os.environ["SF_AGENT_ID"]
def create_session_with_context(token: str, language: str, customer_tier: str) -> str:
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = requests.post(
f"{API_BASE}/agents/{AGENT_ID}/sessions",
headers=headers,
json={
"externalSessionKey": str(uuid.uuid4()),
"instanceConfig": {"endpoint": MY_DOMAIN},
"streamingCapabilities": {"chunkTypes": ["Text"]},
"bypassUser": True,
"variables": [
{"name": "$Context.EndUserLanguage", "type": "Text", "value": language},
{"name": "customerTier", "type": "Text", "value": customer_tier},
],
},
timeout=30,
)
response.raise_for_status()
return response.json()["sessionId"]Here customerTier is an example custom variable. It works only if your agent defines a variable with that name and type.
Sending variables with a message
Values that change during the conversation can go in the message request. Put a variables array beside the message object.
{
"message": {
"sequenceId": 2,
"type": "Text",
"text": "Where is my order?"
},
"variables": [
{ "name": "currentOrderId", "type": "Text", "value": "801xx0000001234" }
]
}Use this when the context belongs to one turn, such as the page the user has open right now.
Common errors
- Name mismatch. A misspelled or undefined variable name means the agent cannot use the value. Copy the name from the agent instead of retyping it.
- Wrong type. Send a value that matches the declared type. A number in a
Textvariable, or the reverse, can cause a failure or an ignored value. - Putting secrets in context. Variable values can be used in prompts and can appear in logs. Send identifiers, not passwords or tokens.
- Trusting the client. If a browser calls your backend and your backend calls the agent, set sensitive variables, such as the customer ID, on the server. Do not accept them from the browser.
Exercise
Your agent defines a custom variable accountId. A user switches to a different account halfway through a conversation. Where do you send the new value, and why?
Show answer
Send it in the variables array of the next message request, not in the session request. The session request is sent only once, so it can't carry a value that changes during the conversation. The message-level variable applies to that turn.