Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Skills Context and State Management

Skills Context and State Management

Each of Claude's API calls is stateless; it doesn't automatically remember the content of the previous session.

But within a continuous conversation, all history messages are in the context window. Understanding this can help you write Skills that perform consistently in conversations.


Where does a Skill's state reside

State types Storage location Valid scope
Conversation history Context window Current session, disappears when closed
Intermediate files /home/claude/ Current session, may be cleared after the task completes
Output files /mnt/user-data/outputs/ Persistent, downloadable by the user
Installed Skills /mnt/skills/ Cross-session persistence

Skills themselves don't have database or persistent storage capabilities. Data that needs to be saved across sessions should be saved by the user (such as downloaded files), or written to a persistent output directory.


Managing multi-turn conversation state in SKILL.md

For Skills that require multi-turn interaction, the flow logic of the conversation state should be clearly defined in SKILL.md.

Example

Multi-Turn Interaction Flow

This Skill operates in three phases:

Phase One: Information Collection

Ask the user to provide the following information:

1

. Data file path

2

. Analysis objective

Do not perform any analysis until all information is obtained.

Display the collected information as a list and tell the user what is still missing.

Phase Two: Confirmation and Execution

After collection is complete, present the execution plan and wait for user confirmation:

I will perform analysis on

{

File Name

}

Executing

{

analysis objective

}

, estimated to take about

{

Time

}

seconds. Confirm to continue?

Phase Three: Execution and Output

Execute after user confirmation, report progress in real time, and output the results upon completion.

If the user says at any phase

"Restart"

, 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 pass state 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 the current execution state to a file"""

    state

[

"updated_at"

]

=

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 the last saved state; if it does not exist, return the initial state"""

   

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 (for the "Restart" scenario)"""

   

if

os

.

path

.

exists

(

STATE_FILE

)

:

       

os

.

remove

(

STATE_FILE

)

   

print

(

"State cleared, restarting"

)

Usage examples

if

name

==

"main"

:

   

Load or initialize state

    state

=

load_state

(

)

   

print

(

f

"Current phase: {state['phase']}"

)

   

Update state: file path collected

    state

[

"file_path"

]

=

"/mnt/user-data/uploads/runoob_data.csv"

    state

[

"phase"

]

   

=

"confirming"

    save_state

(

state

)



状态已保存:/home/claude/skill_state.json

当前阶段:collecting

状态已保存:/home/claude/skill_state.json


Preventing Context Pollution

When a Skill is triggered multiple times in the same conversation, old parameters from the conversation history may "pollute" the new execution.

Clearly defining priority in SKILL.md can avoid such problems.

Example

Parameter Priority Rules

If multiple sets of parameters exist in the same conversation, handle 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 the conversation history

**

-> Low priority, used only when not specified in the current message

4

.

**

Default values defined by the Skill

**

-> Fallback

If there is any doubt about the source of a parameter, confirm with the user before execution:

"I will use runoob_v2.csv (the file you just uploaded) instead of the previous runoob_v1.csv. Is that correct?"


The Relationship Between Context Length and Performance

As conversations grow longer and more content fills the context window, the cost of processing by Claude also increases.

For long-running multi-turn Skills, the following optimization strategies should be noted:

Strategy Approach effect
Avoid repeatedly outputting large blocks of content Reference previously output file paths instead of re-outputting the content Reduce context usage
Use files to transfer intermediate data Write large datasets to files and pass only paths in the conversation Avoid large data filling up the context
Timely inform the user of phase completion After each phase ends, explicitly say "Step One complete" Let the user know the progress to reduce follow-up questions

The context window is a limited resource. The more data passed in a Skill, the less space remains for reasoning and generation, and response quality may decline. For large files, always pass them by file path rather than by content.