Context Variables
Context Variables
Every customer is unique, and your agent should treat them accordingly where appropriate.
Context variables enrich the context that the agent sees about the customer it's talking to. They are meant to give your agent awareness to information that helps it personalize its service, much like how a seasoned customer service representative knows and remembers important details about each client.
When a customer interacts with your agent, their context variables are automatically loaded, allowing the agent to tailor its responses based on their specific situation.
Real World Applications
Let's walk through how context variables transform customer interactions. Imagine you're running a SaaS platform's support agent. You might track variables like subscription plan, last login date, which features each customer uses, and their company size.
Consider two different customers reaching out about data exports. Sarah, a startup founder on the free plan, asks "Can I export my data to Excel?" Your agent, aware of her free plan status, can respond thoughtfully: "While Excel export is a premium feature, I can show you how to use our basic CSV export. Would you also like to learn about the advanced reporting capabilities in our premium plan?"
Now imagine Tom from an enterprise account reaches out with the same question. The agent sees his enterprise status but also notices his team hasn't explored many advanced features. It might respond: "I'll help you with Excel exports! I notice your team hasn't tried our automated reporting suite yet - this is included in your enterprise plan and could save you hours each week. Would you like me to show you both features?"
Working with Context Variables
A single context variable identifies a particular piece of information. For example, you might create a variable called "subscription_plan"
.
Each customer can then have a unique value assigned to them, under that context variable. For example, Tom might have "enterprise"
as the variable's value.
Here's how to create a new context variable and assign a value to a customer:
- CLI
- Python
- TypeScript
$ parlant variable create \
--agent-id AGENT_ID \
--name VARIABLE_NAME \
--description VARIABLE_DESCRIPTION # Optional
$ parlant variable set \
--agent-id AGENT_ID \
--id VARIABLE_ID \
--key CUSTOMER_ID \
--value VARIABLE_VALUE
from parlant.client import ParlantClient
client = ParlantClient(base_url=SERVER_ADDRESS)
variable = client.context_variables.create(
agent_id=AGENT_ID,
name=VARIABLE_NAME,
description=VARIABLE_DESCRIPTION,
)
client.context_variables.set_value(
agent_id=AGENT_ID,
variable_id=variable.id,
key=CUSTOMER_ID,
data=VARIABLE_VALUE,
)
import { ParlantClient } from 'parlant-client';
const client = new ParlantClient({ environment: SERVER_ADDRESS });
const variable = await client.contextVariables.create(AGENT_ID, {
name: VARIABLE_NAME,
description: VARIABLE_DESCRIPTION,
});
await client.contextVariables.setValue(AGENT_ID, variable.id, CUSTOMER_ID, {
data: VARIABLE_VALUE,
});
Alternatively, you can also create tags for customers—say, to assign them to groups—and then to configure variable values for tags (customer groups) instead of individual customers.
- CLI
- Python
- TypeScript
$ parlant variable create \
--agent-id AGENT_ID \
--name VARIABLE_NAME \
--description VARIABLE_DESCRIPTION # Optional
$ parlant variable set \
--agent-id AGENT_ID \
--id VARIABLE_ID \
--key CUSTOMER_ID \
--value VARIABLE_VALUE
from parlant.client import ParlantClient
client = ParlantClient(base_url=SERVER_ADDRESS)
variable = client.context_variables.create(
agent_id=AGENT_ID,
name=VARIABLE_NAME,
description=VARIABLE_DESCRIPTION,
)
client.context_variables.set_value(
agent_id=AGENT_ID,
variable_id=variable.id,
key=CUSTOMER_ID,
data=VARIABLE_VALUE,
)
import { ParlantClient } from 'parlant-client';
const client = new ParlantClient({ environment: SERVER_ADDRESS });
const variable = await client.contextVariables.create(AGENT_ID, {
name: VARIABLE_NAME,
description: VARIABLE_DESCRIPTION,
});
await client.contextVariables.setValue(AGENT_ID, variable.id, CUSTOMER_ID, {
data: VARIABLE_VALUE,
});
Combining Context Variables with Guidelines
Let's explore how guidelines and context variables work together to create truly intelligent interactions. Imagine you're running an AI support agent for a digital bank where customers have different account tiers and transaction patterns.
Here's a focused guideline:
- Condition: customer's account tier is 'basic' AND they ask about instant international transfers
- Action: Highlight our same-day domestic transfers that are free on their plan, then mention how premium enables instant global payments
When Mark asks about sending money to his daughter studying abroad, instead of a flat "that's premium only" response, he hears: "I can help you send that money today using our standard international transfer. By the way, our premium accounts get this done instantly with lower fees—would you like to know more?"
For high-value customers who haven't explored wealth management:
- Condition: customer's balance is at least 100k AND they aren't using our investment products
- Action: After you're done helping them with their inquiry, mention our personalized investment advisory service
So when Jessica checks her substantial balance, the agent notes: "I see you've built up some significant savings. Did you know our wealth management team can help create a personalized investment strategy for you?"