Context
The Context object is the "blood" of the agent system. It flows through execution steps, carrying all necessary state, configuration, and history.
Crucially, Agentswarm treats Context as immutable and isolated regarding execution flow. When an agent forks or recurses, it typically operates on a copy of the context, ensuring that its local state changes (like thought processes or temporary variables) do not pollute the parent context or parallel execution branches unless explicitly merged back (usually via the Store).
Key Components
A Context instance encapsulates:
- Messages: The conversation history (User messages, System prompts, Assistant replies).
- Store: A reference to the shared data store (see Store).
- Tracing: A reference to the tracing mechanism (see Tracing).
- Feedback: A reference to the feedback system (see Feedback).
- Thoughts: Internal reasoning steps generated by the agent during its execution.
- Trace ID / Step ID: Identifiers for observability and debugging.
API Reference
agentswarm.datamodels.Context
The Context class contains all the information of the current context, with the messages, store, usage and so on. Moreover, the Context contains informations about the tracing and current execution step.
Source code in src/agentswarm/datamodels/context.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
add_usage(usage)
Add usage to the current context
Source code in src/agentswarm/datamodels/context.py
110 111 112 113 114 | |
copy_for_execution()
Copy the current context for a new (clean) execution. The new context will have a cleaned messages list and thoughts, and will have a new step_id. The parent_step_id of the new context will be the current step_id, in order to trace the execution hierarchy.
The store and the default_llm will remain the same.
Source code in src/agentswarm/datamodels/context.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
copy_for_iteration(step_id, messages)
Copy the current context for a new iteration with the specified step_id and messages. The new context will have the same messages and thoughts. The parent_step_id of the new context will be the current step_id, in order to trace the iteration hierarchy.
The store and the default_llm will remain the same.
Source code in src/agentswarm/datamodels/context.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
emit_feedback(payload, source=None)
Emit a feedback event. If source is not specified, it could be inferred from the current context state if needed.
Source code in src/agentswarm/datamodels/context.py
197 198 199 200 201 202 203 204 | |
from_dict(data, default_llm=None)
classmethod
Deserializes the context from a dictionary.
Source code in src/agentswarm/datamodels/context.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
merge(remote_context, base_messages_count=None, base_thoughts_count=None, base_usage_count=None)
Merges the state from a remote context into this context. Ensures that messages, thoughts, and usage are appended/updated without breaking references.
Source code in src/agentswarm/datamodels/context.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
to_dict()
Serializes the context to a dictionary.
Source code in src/agentswarm/datamodels/context.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |