Context API¶
API reference for conversation context management and strategies.
ContextManager¶
Manages conversation history and context for LLM interactions.
Constructor¶
Parameters:
max_tokens
- Maximum context size in tokenssystem_prompt
- System instructions for LLM
Methods¶
add_message()¶
Add SPADE message to conversation context.
Example:
context = ContextManager(system_prompt="You are helpful")
context.add_message(spade_message, "user1_session")
add_message_dict()¶
Add message from dictionary format.
Example:
user_msg = {"role": "user", "content": "Hello!"}
context.add_message_dict(user_msg, "user1_session")
add_assistant_message()¶
Add assistant response to context.
Example:
add_tool_result()¶
def add_tool_result(
self,
tool_name: str,
result: Any,
tool_call_id: str,
conversation_id: Optional[str] = None
) -> None
Add tool execution result to context.
Example:
context.add_tool_result(
tool_name="get_weather",
result="22°C, sunny",
tool_call_id="call_123",
conversation_id="user1_session"
)
get_prompt()¶
Get formatted prompt for LLM provider.
Example:
get_conversation_history()¶
Get raw conversation history.
Example:
history = context.get_conversation_history("user1_session")
print(f"Conversation has {len(history)} messages")
clear()¶
Clear conversation messages.
Example:
# Clear specific conversation
context.clear("user1_session")
# Clear all conversations
context.clear("all")
get_active_conversations()¶
Get list of active conversation IDs.
Example:
set_current_conversation()¶
Set current conversation context.
Example:
Example Usage¶
from spade_llm.context import ContextManager
# Create context manager
context = ContextManager(
system_prompt="You are a helpful coding assistant",
max_tokens=2000
)
# Add conversation messages
context.add_message_dict(
{"role": "user", "content": "Help me with Python"},
"coding_session"
)
context.add_assistant_message(
"I'd be happy to help with Python!",
"coding_session"
)
# Get formatted prompt
prompt = context.get_prompt("coding_session")
# Use with LLM provider
Context Management Strategies¶
ContextManagement (Abstract Base)¶
Base class for all context management strategies.
apply_context_strategy()¶
def apply_context_strategy(
self,
messages: List[ContextMessage],
system_prompt: Optional[str] = None
) -> List[ContextMessage]
Apply the context management strategy to messages.
get_stats()¶
Get statistics about context management.
NoContextManagement¶
Preserves all messages without any filtering.
WindowSizeContext¶
Basic sliding window context management.
Parameters:
- max_messages
(int): Maximum number of messages to keep
SmartWindowSizeContext¶
Context management with message selection.
from spade_llm.context import SmartWindowSizeContext
context = SmartWindowSizeContext(
max_messages=20,
preserve_initial=3,
prioritize_tools=True
)
Parameters:
- max_messages
(int): Maximum number of messages to keep
- preserve_initial
(int, optional): Number of initial messages to always preserve
- prioritize_tools
(bool, optional): Whether to prioritize tool result messages
Example:
# Smart context with tool prioritization
smart_context = SmartWindowSizeContext(
max_messages=25,
preserve_initial=3,
prioritize_tools=True
)
# Get statistics
stats = smart_context.get_stats(total_messages=50)
# Returns: {"strategy": "smart_window_size", "max_messages": 25, ...}
Message Types¶
ContextMessage Types¶
from spade_llm.context._types import (
SystemMessage,
UserMessage,
AssistantMessage,
ToolResultMessage
)
SystemMessage¶
UserMessage¶
AssistantMessage¶
# Text response
{
"role": "assistant",
"content": "I'm doing well, thank you!"
}
# With tool calls
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Madrid\"}"
}
}
]
}