This document provides a complete reference for the Context API in both Python and TypeScript.
Note: Both implementations provide identical functionality with language-appropriate naming conventions (snake_case for Python, camelCase for TypeScript).
The main Context class for managing execution contexts.
class Context:
def __init__(
self,
intent: str,
category: Optional[str] = None,
constraints: Optional[Dict[str, Any]] = None,
routing: Optional[Dict[str, Any]] = None,
output: Optional[Dict[str, Any]] = None,
metadata: Optional[Dict[str, Any]] = None,
parent_id: Optional[str] = None,
context_id: Optional[str] = None
)Parameters:
intent(str): Categorical intent (e.g., "analyze", "summarize", "extract")category(str, optional): Discrete category for this executionconstraints(dict, optional): Hard limits (max_tokens, max_time, max_cost)routing(dict, optional): Routing hints (model, provider, strategy, temperature)output(dict, optional): Output shaping (format, schema)metadata(dict, optional): Arbitrary metadataparent_id(str, optional): Parent context ID if extendedcontext_id(str, optional): Explicit context ID (auto-generated if not provided)
class Context {
constructor(config: ContextConfig)
}
interface ContextConfig {
intent: string;
category?: string;
constraints?: Record<string, any>;
routing?: Record<string, any>;
output?: Record<string, any>;
metadata?: Record<string, any>;
parentId?: string;
contextId?: string;
}Add an input to the context.
Python:
def add_input(
self,
data: Any,
relevance: float = 1.0,
tokens: Optional[int] = None
) -> ContextTypeScript:
addInput(
data: any,
options?: { relevance?: number; tokens?: number }
): ContextParameters:
data: Input data (any type)relevance: Relevance score 0.0 to 1.0 (default: 1.0)tokens: Token count (auto-estimated if not provided)
Returns: Self for method chaining
Example:
ctx.add_input({"title": "Book 1"}, relevance=0.9)
ctx.add_input({"title": "Book 2"}, relevance=0.7)Prune inputs to fit constraints.
Python:
def prune(
self,
max_tokens: Optional[int] = None,
relevance_threshold: float = 0.0
) -> ContextTypeScript:
prune(options?: {
maxTokens?: number;
relevanceThreshold?: number;
}): ContextParameters:
max_tokens/maxTokens: Maximum tokens to keep (uses constraint if not provided)relevance_threshold/relevanceThreshold: Minimum relevance to keep (default: 0.0)
Returns: Self for method chaining
Behavior:
- Filters inputs below relevance threshold
- Sorts remaining inputs by relevance (descending)
- Keeps inputs until token limit is reached
- May truncate final text input to fit
Example:
ctx.prune(max_tokens=2000, relevance_threshold=0.5)Update routing configuration.
Python:
def route(
self,
model: Optional[str] = None,
provider: Optional[str] = None,
strategy: Optional[str] = None
) -> ContextTypeScript:
route(options?: {
model?: string;
provider?: string;
strategy?: string;
}): ContextParameters:
model: Model identifier ("gpt-4", "gpt-3.5-turbo", "claude-3-opus", etc.)provider: Provider identifier ("openai", "anthropic", "local")strategy: Routing strategy ("cost_optimized", "quality_optimized", "speed_optimized")
Returns: Self for method chaining
Strategies:
cost_optimized: Selects cheapest modelquality_optimized: Selects highest quality modelspeed_optimized: Selects fastest model
Example:
ctx.route(strategy="cost_optimized")
# or
ctx.route(model="gpt-4", provider="openai")Execute the context with a task.
Python:
def execute(
self,
task: str,
system_prompt: Optional[str] = None,
override_routing: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None
) -> Dict[str, Any]TypeScript:
async execute(request: {
task: string;
systemPrompt?: string;
overrideRouting?: Record<string, any>;
apiKey?: string;
}): Promise<ExecutionResponse>Parameters:
task: Task description or promptsystem_prompt/systemPrompt: Optional system promptoverride_routing/overrideRouting: Override routing for this executionapi_key/apiKey: API key for the provider (user-provided)
Returns: Execution response object
Response Structure:
{
result: any, // Execution result
contextId: string, // Context identifier
modelUsed: string, // Model that was used
providerUsed: string, // Provider that was used
duration: number, // Execution duration in seconds
metadata?: { // Additional metadata
intent: string,
inputCount: number,
totalInputTokens: number
}
}Example:
result = ctx.execute(
task="Analyze this data",
system_prompt="You are a helpful analyst",
api_key=os.environ["OPENAI_API_KEY"]
)
print(result["result"])Create a child context extending this one.
Python:
def extend(
self,
intent: Optional[str] = None,
**kwargs
) -> ContextTypeScript:
extend(config?: Partial<ContextConfig>): ContextParameters:
intent: New intent (inherits parent if not provided)- Additional context parameters (category, constraints, routing, etc.)
Returns: New child context
Behavior:
- Inherits all inputs from parent
- Inherits constraints, routing, and metadata
- Sets parent_id to parent's ID
- Can override any inherited values
Example:
parent = Context(intent="analyze", constraints={"max_tokens": 2000})
parent.add_input(base_data)
child = parent.extend(intent="summarize")
child.add_input(additional_data)
# child has both base_data and additional_dataMerge another context into a new context.
Python:
def merge(self, other: Context) -> ContextTypeScript:
merge(other: Context): ContextParameters:
other: Context to merge
Returns: New merged context
Behavior:
- Combines inputs from both contexts
- Uses most restrictive constraints
- Other's routing takes precedence
- Merges metadata
Example:
ctx1 = Context(intent="analyze", constraints={"max_tokens": 2000})
ctx1.add_input(data1)
ctx2 = Context(intent="analyze", constraints={"max_tokens": 3000})
ctx2.add_input(data2)
merged = ctx1.merge(ctx2)
# merged has both data1 and data2
# merged.constraints["max_tokens"] == 2000 (most restrictive)Serialize to JSON string.
Python:
def to_json(self) -> strTypeScript:
toJSON(): ContextDataReturns: JSON string (Python) or plain object (TypeScript)
Example:
# Python
json_str = ctx.to_json()
with open("context.json", "w") as f:
f.write(json_str)
// TypeScript
const data = ctx.toJSON();
const jsonStr = JSON.stringify(data);
localStorage.setItem('context', jsonStr);Deserialize from JSON string.
Python:
@classmethod
def from_json(cls, json_str: str) -> ContextTypeScript:
static fromJSON(data: ContextData): ContextParameters:
json_str/data: JSON string or object
Returns: Context instance
Example:
# Python
with open("context.json") as f:
ctx = Context.from_json(f.read())
// TypeScript
const jsonStr = localStorage.getItem('context');
const ctx = Context.fromJSON(JSON.parse(jsonStr));Get total token count for all inputs.
Python:
def get_total_tokens(self) -> intTypeScript:
getTotalTokens(): numberReturns: Total token count
Example:
total = ctx.get_total_tokens()
print(f"Total tokens: {total}")Input pruning and selection logic.
Python:
class Pruner:
def prune(
self,
inputs: List[ContextInput],
max_tokens: Optional[int] = None,
relevance_threshold: float = 0.0
) -> List[ContextInput]TypeScript:
class Pruner {
prune(
inputs: ContextInput[],
maxTokens?: number,
relevanceThreshold?: number
): ContextInput[]
}Note: Typically not used directly; use Context.prune() instead.
Model and provider routing logic.
Python:
class Router:
def route(
self,
current_routing: Dict[str, Any],
model: Optional[str] = None,
provider: Optional[str] = None,
strategy: Optional[str] = None
) -> Dict[str, Any]
def get_model_spec(self, model: str) -> Dict[str, Any]TypeScript:
class Router {
route(
currentRouting: Record<string, any>,
model?: string,
provider?: string,
strategy?: string
): Record<string, any>
getModelSpec(model: string): ModelSpec | undefined
}Note: Typically not used directly; use Context.route() instead.
Execution engine for Context.
Python:
class Executor:
def execute(
self,
context: Context,
request: Dict[str, Any],
api_key: Optional[str] = None
) -> Dict[str, Any]TypeScript:
class Executor {
async execute(
context: Context,
request: ExecutionRequest
): Promise<ExecutionResponse>
}Note: Typically not used directly; use Context.execute() instead.
Python:
class ContextInput:
data: Any
relevance: float
tokens: intTypeScript:
class ContextInput {
data: any;
relevance: number;
tokens: number;
}Common constraint keys:
max_tokens/maxTokens: Maximum tokens for contextmax_time/maxTime: Maximum execution time in secondsmax_cost/maxCost: Maximum cost in USD
Common routing keys:
model: Model identifierprovider: Provider identifierstrategy: Routing strategytemperature: Model temperature (0.0 to 2.0)max_output_tokens/maxOutputTokens: Maximum output tokens
Common output keys:
format: Output format ("json", "markdown", "text", "html")schema: Output schema for structured formats
Current model specifications (can be extended):
| Model | Provider | Max Tokens | Quality | Speed | Cost (per 1k input) |
|---|---|---|---|---|---|
| gpt-4 | openai | 8192 | 0.95 | 0.6 | $0.03 |
| gpt-3.5-turbo | openai | 4096 | 0.75 | 0.9 | $0.0015 |
| claude-3-opus | anthropic | 4096 | 0.95 | 0.7 | $0.015 |
| claude-3-sonnet | anthropic | 4096 | 0.85 | 0.85 | $0.003 |
Python:
try:
result = ctx.execute(task="...", api_key=api_key)
except Exception as e:
print(f"Execution failed: {e}")TypeScript:
try {
const result = await ctx.execute({ task: "...", apiKey });
} catch (error) {
console.error('Execution failed:', error);
}- Always set constraints to prevent runaway costs
- Use relevance scores to prioritize inputs
- Serialize contexts for reproducibility
- Choose appropriate strategies for your use case
- Handle errors gracefully in production
See the examples/ directory for complete working examples.