Skills Context
Each API call to Claude is stateless; it doesn't automatically remember the content of the previous session.
However, in a continuous conversation, all historical messages are within the context window. Understanding this helps you write Skills that perform stably in conversations.
* * *
## Where Skills State Exists
| State Type | Storage Location | Valid Range |
| --- | --- | --- |
| Conversation History | Context Window | Current session, disappears after closing |
| Intermediate Files | `/home/claude/` | Current session, may be cleared after task completion |
| Output Files | `/mnt/user-data/outputs/` | Persistent, user can download |
| Installed Skills | `/mnt/skills/` | Persistent across sessions |
> Skills themselves do not have database or persistent storage capabilities. Data that needs to be saved across sessions should be saved by the user themselves (such as downloading files), or written to the persistent output directory.
* * *
## Managing Multi-turn Conversation State in SKILL.md
For Skills that require multi-turn interaction, the conversation state transition logic should be clearly defined in SKILL.md.
## Example
## Multi-turn Interaction Process
This Skill runs in three phases:
### Phase One: Information Collection
Ask the user to provide the following information:
1. Data file path
2. Analysis target
Do not execute analysis until all information is obtained.
Display the collected information in a list format, informing the user what is still missing.
### Phase Two: Confirm Execution
After collection is complete, display the execution plan and wait for user confirmation:
> I will perform {analysis target} on {file name}, which is expected to take about {time} seconds. Confirm to continue?
### Phase Three: Execute and Output
After user confirmation, execute while informing progress in real-time, and output results after completion.
If the user says "restart" at any stage, clear the currently collected information and return to phase one.
* * *
## Using Files to Save Intermediate State
For complex multi-step tasks, you can write intermediate state to a JSON file to achieve state transfer between steps.
## Example
# File Path: scripts/state_manager.py
import json
import os
from datetime import datetime
# State file storage path (current session working directory)
STATE_FILE ="/home/claude/skill_state.json"
def save_state(state: dict) ->None:
"""Save current execution state to file"""
state=datetime.now().isoformat()
with open(STATE_FILE,"w", encoding="utf-8")as f:
json.dump(state, f, ensure_ascii=False, indent=2)
print(f"State saved: {STATE_FILE}")
def load_state() ->dict:
"""Load previously saved state, return initial state if not exists"""
if not os.path.exists(STATE_FILE):
return{
"phase": "collecting",# Current phase: collecting / confirming / executing / done
"file_path": None,
"target": None,
"result": None,
"created_at": datetime.now().isoformat()
}
with open(STATE_FILE,"r", encoding="utf-8")as f:
return json.load(f)
def clear_state() ->None:
"""Clear state (used for "restart" scenario)"""
if os.path.exists(STATE_FILE):
os.remove(STATE_FILE)
print("State cleared, restarting")
# Usage example
if __name__ =="__main__":
# Load or initialize state
state = load_state()
print(f"Current phase: {state['phase']}")
# Update state: file path collected
state="/mnt/user-data/uploads/tutorial_data.csv"
state="confirming"
save_state(state)
State saved: /home/claude/skill_state.json Current phase: collecting State saved: /home/claude/skill_state.json
* * *
## Preventing Context Pollution
When a user triggers a Skill multiple times in the same conversation, old parameters from historical conversations may "pollute" new executions.
Clearly defining priority rules in SKILL.md can avoid such problems.
## Example
## Parameter Priority Rules
If multiple sets of parameters exist in the same conversation, process them according to the following priority:
1. **Parameters explicitly specified in the user's latest message** β Highest priority, always use
2. **Files uploaded in this request** β High priority
3. **Parameters mentioned in conversation history** β Low priority, only used when current message doesn't specify
4. **Default values defined by Skill** β Fallback
If there are doubts about parameter sources, confirm with the user before execution: "I will use tutorial_v2.csv (the file you just uploaded) instead of the previous tutorial_v1.csv, is that correct?"
* * *
## Relationship Between Context Length and Performance
As the conversation grows longer, there is more and more content in the context window, and the cost of Claude processing also increases.
For long-running multi-turn Skills, pay attention to the following optimization strategies:
| Strategy | Approach | Effect |
| --- | --- | --- |
| Avoid repeatedly outputting large blocks | Reference previously output file paths instead of re-outputting content | Reduce context usage |
| Use files to pass intermediate data | Write large datasets to files, only pass paths in conversation | Avoid large data filling up context |
| Timely inform user of phase completion | After each phase ends, clearly say "Step one completed" | Let user know progress, reduce follow-up questions |
> The context window is a limited resource. The more data passed in a Skill, the less space remains for inference and generation, and response quality may degrade. For large files, always pass via file path rather than content.
YouTip