How to Use Coze Variables and Long‑Term Memory in Low‑Code Agent Development (Part 6)
This guide walks through Coze’s variable and long‑term memory features, showing how to define and use variables for user data, configure persistent memory, and combine both to build personalized, context‑aware AI agents, with step‑by‑step screenshots, code snippets, and real‑world scenarios.
Coze Variables
Variables store user information and guide dialogue flow. Example: variable user_name records the name from the first interaction and can be reused to greet the user on subsequent visits. Variable user_visits increments on each visit to differentiate greetings for first‑time and returning users.
Setup Steps
Open Coze platform at https://www.coze.cn/home and create a new agent.
In the orchestration view, locate the “Variables” section and click the “+” button to add a variable.
Enter variable name, default value, and description, then save.
System preset variables such as sys_uuid, sys_longitude, sys_latitude are read‑only.
Create a variable visit_times to record the number of user visits.
Prompt Logic Example
If visit_times = 1, ask for the user’s name.
If visit_times > 1 and name exists, greet with “{{name}}, welcome back for the {{visit_times}}‑th time!”
If visit_times > 1 and name is missing, ask for the name again.
After each greeting, visit_times increments by 1.
# Role
You are a smart greeting assistant that greets based on visit count.
## Skills
### Skill 1: First‑visit greeting
1. When `visit_times` = 1, output: "你好,我是你的智能问候助手,请问怎么称呼你?"
### Skill 2: Non‑first‑visit greeting
1. When `visit_times` > 1 and `name` exists, output: "{{name}},欢迎第{{visit_times}}次光临!很高兴再次见到你。"
2. When `visit_times` > 1 and `name` missing, output: "欢迎第{{visit_times}}次光临!我是你的智能问候助手,请问怎么称呼你?"
## Constraints
- After each greeting, `visit_times` increments by 1.
- Follow the given conditions and output format exactly.Long‑Term Memory
Long‑term memory records every dialogue turn, extracts key information, and stores it as structured memory data. When a user returns, the agent can recall past interactions to generate coherent, personalized responses.
Enabling
For agents created before 2025‑10‑13, enable long‑term memory with a single click in the orchestration page. For newer agents, first create a memory store, then bind it in the agent configuration.
Retail Scenario
A user asks about a summer dress. The agent remembers the color preference, suggested size, and fabric‑care advice in the next session, replying:
“Welcome back! I remember last week we discussed the summer dress you liked—light‑blue French‑print. Based on your height and weight, I suggested size S, but you might prefer XS for a slimmer fit. Note the fabric should not be soaked long to avoid deformation…”
Combined Use
Variables act as a static user profile (e.g., name, age, gender, height, weight). Long‑term memory captures dynamic health data and conversation history. In a health‑management assistant, the agent extracts statements about appetite, sleep, and diet, stores them in memory, and later generates a personalized recommendation that references both variables and memory points.
{{user_name}},根据您的年龄({{user_age}}岁)和体重({{user_weight}}kg),我建议从以下几个方面进行调整:
1. **改善睡眠质量**
尽量在晚上11点前入睡,保证每晚睡眠不少于7小时。您之前提到过睡眠不足的问题,可以尝试睡前泡热水澡或进行呼吸放松练习。
2. **优化饮食结构**
多吃富含维生素C和E的新鲜蔬果,减少高糖高脂摄入。关于之前讨论的减脂食谱,如果您在执行中遇到困难,我们可以进一步调整方案。
3. **加入适度运动**
建议进行低强度有氧运动,如慢跑、游泳等,有助于促进新陈代谢、改善血液循环。
4. **针对性补充营养**
可适当食用柑橘类水果,或考虑服用维生素C与E的补充剂,有助于改善皮肤状态。
如果您在执行过程中有任何不适或新的情况,欢迎随时告诉我,我将根据您的进展提供进一步建议。Summary
Variables provide static user attributes; long‑term memory preserves evolving context across sessions. Together they enable personalized, multi‑turn interactions. Coze also supports integration with structured databases for richer data management.
Fun with Large Models
Master's graduate from Beijing Institute of Technology, published four top‑journal papers, previously worked as a developer at ByteDance and Alibaba. Currently researching large models at a major state‑owned enterprise. Committed to sharing concise, practical AI large‑model development experience, believing that AI large models will become as essential as PCs in the future. Let's start experimenting now!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
