In stateful applications, not all data needs to be persisted throughout the entire conversation history. Some data is "ephemeral"—it is relevant only for the immediate next step or within a specific super-step (parallel execution block) and should be discarded afterwards. Examples include temporary search results, intermediate reasoning steps, or flags that trigger immediate actions but shouldn't confuse future turns. Ephemeral Channels provide a way to manage this lifecycle automatically.
Features
Automatic Cleanup: Channels marked as ephemeral are automatically cleared from the state after the step completes.
Scope Isolation: Prevents temporary data from leaking into future execution steps, reducing context pollution.
Configurable: Defined via RegisterChannel with an isEphemeral flag.
Implementation Principle
This feature is implemented via the CleaningStateSchema interface in graph/schema.go.
MapSchema maintains a set of EphemeralKeys.
During the graph execution loop (InvokeWithConfig), after all nodes in the current step have executed and their results merged:
The Cleanup(state) method is called.
This method returns a new state map with all keys present in EphemeralKeys removed.
We explicitly tell the schema that temp_data should be treated as ephemeral.
Producer Node:
Sets temp_data to "secret_code_123".
Consumer Node:
Checks for temp_data. Since the producer runs in Step 1, and the consumer runs in Step 2 (sequentially), the cleanup logic triggers between them.
Note: In LangGraph semantics, ephemeral values are cleared after the step. If Producer -> Consumer is a direct transition, they might be considered separate steps.
Verification:
The output shows that the consumer (running in the next step) does not see the temp_data, confirming it was cleaned up.