Context¶
AgentContext¶
AgentContext
dataclass
¶
Execution context for an agent operation.
Carries metadata about the current request and provides the context window for LLM prompt assembly.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id |
str
|
Unique identifier for tracing this operation. |
endpoint_name |
str
|
The agent endpoint handling this request. |
session_id |
str | None
|
Optional session identifier for multi-turn conversations. |
user_id |
str | None
|
Optional user identifier. |
metadata |
dict[str, Any]
|
Arbitrary key-value metadata. |
context_window |
ContextWindow
|
The context window for LLM prompts. |
memory |
MemoryStore | None
|
Phase C1 — optional :class: |
Source code in src/agenticapi/runtime/context.py
add_context ¶
Add a context item for use in future LLM prompts.
Allows agents to store learned information back into the context window for multi-turn conversations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for this context item. |
required |
value
|
str
|
The context content. |
required |
source
|
str
|
Origin of this context (e.g., "agent", "tool", "user"). |
'agent'
|
priority
|
int
|
Higher priority items are included first in prompts. |
0
|
Source code in src/agenticapi/runtime/context.py
ContextWindow¶
ContextWindow
dataclass
¶
Manages a window of context items within a token budget.
Items are sorted by priority (descending) when building the final context string. Provides token estimation for budget management.
Attributes:
| Name | Type | Description |
|---|---|---|
max_tokens |
int
|
Maximum estimated tokens for the context window. |
items |
list[ContextItem]
|
The context items in this window. |
Source code in src/agenticapi/runtime/context.py
add ¶
Add a context item to the window.
Items that would exceed the token budget are silently dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
ContextItem
|
The context item to add. |
required |
Source code in src/agenticapi/runtime/context.py
build ¶
Build the final context string from all items.
Items are sorted by priority (highest first) and concatenated with section headers.
Returns:
| Type | Description |
|---|---|
str
|
The assembled context string. |
Source code in src/agenticapi/runtime/context.py
estimated_tokens ¶
Estimate the total token count of all items.
Uses a rough heuristic of 1 token per 4 characters.
Returns:
| Type | Description |
|---|---|
int
|
Estimated token count. |
Source code in src/agenticapi/runtime/context.py
ContextItem¶
ContextItem
dataclass
¶
A single item of context to include in the LLM prompt.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
Unique identifier for this context item. |
value |
str
|
The text content of this context. |
source |
str
|
Where this context originated from (e.g., "session", "tool", "user"). |
priority |
int
|
Higher priority items are included first (default 0). |