Agents
For usage examples, see Usage - Agents.
AgentsClient
Client for running agents via the Mentiora API.
Note: Unlike tracing methods (which return SendTraceResult and never throw), agent methods throw exceptions on errors (ValidationError, NetworkError).
Methods
- TypeScript
- Python
run(params: AgentRunParams): Promise<AgentRunResult>
Run an agent and return the complete result.
Parameters:
params: AgentRunParams- Agent run parameters
Returns: Promise<AgentRunResult>
Throws: ValidationError, NetworkError
Example:
const result = await client.agents.run({
tag: 'production',
message: 'What is the weather today?',
});
console.log(result.output);
stream(params: AgentRunParams): AsyncGenerator<AgentStreamEvent>
Run an agent with streaming. Returns an async iterable of events.
Parameters:
params: AgentRunParams- Agent run parameters
Yields: AgentStreamEvent objects
Throws: ValidationError, NetworkError
Example:
for await (const event of client.agents.stream({
tag: 'production',
message: 'Write a poem about TypeScript.',
})) {
if (event.type === 'output_text_delta') {
process.stdout.write(event.delta);
} else if (event.type === 'error') {
console.error(`Error: ${event.message}`);
}
}
run(params: AgentRunParams) -> AgentRunResult
Run an agent synchronously and return the complete result.
Parameters:
params: AgentRunParams- Agent run parameters
Returns: AgentRunResult
Raises: ValidationError, NetworkError
Example:
from mentiora import AgentRunParams
result = client.agents.run(AgentRunParams(
tag='production',
message='What is the weather today?',
))
print(result.output)
run_async(params: AgentRunParams) -> AgentRunResult
Run an agent asynchronously and return the complete result.
Parameters:
params: AgentRunParams- Agent run parameters
Returns: AgentRunResult
Raises: ValidationError, NetworkError
Example:
result = await client.agents.run_async(AgentRunParams(
tag='production',
message='What is the weather today?',
))
print(result.output)
stream(params: AgentRunParams) -> Iterator[AgentStreamEvent]
Run an agent with streaming (synchronous). Yields typed events as they arrive.
Parameters:
params: AgentRunParams- Agent run parameters
Yields: AgentStreamEvent objects
Raises: ValidationError, NetworkError
Example:
for event in client.agents.stream(AgentRunParams(
tag='production',
message='Write a poem about Python.',
)):
if event.type == 'output_text_delta':
print(event.delta, end='', flush=True)
elif event.type == 'error':
print(f'Error: {event.message}')
stream_async(params: AgentRunParams) -> AsyncIterator[AgentStreamEvent]
Run an agent with streaming (asynchronous). Yields typed events as they arrive.
Parameters:
params: AgentRunParams- Agent run parameters
Yields: AgentStreamEvent objects
Raises: ValidationError, NetworkError
Example:
async for event in client.agents.stream_async(AgentRunParams(
tag='production',
message='Write a poem about Python.',
)):
if event.type == 'output_text_delta':
print(event.delta, end='', flush=True)
elif event.type == 'error':
print(f'Error: {event.message}')
Types
AgentRunParams
- TypeScript
- Python
interface AgentRunParams {
tag?: string; // Tag name to resolve agent (e.g. 'production')
agentId?: string; // Explicit agent ID (alternative to tag)
revision?: number; // Explicit revision number (used with agentId)
message: string; // User message to send (required)
threadId?: string; // Thread ID for multi-turn conversations
modelId?: string; // Override the agent's default model
modelParams?: {
// Override model parameters
temperature?: number;
maxTokens?: number;
seed?: number;
};
endUserId?: string; // End-user identifier for tracking
metadata?: Record<string, unknown>; // Arbitrary metadata
}
class AgentRunParams:
tag: str | None # Tag name to resolve agent (e.g. 'production')
agent_id: str | None # Explicit agent ID (alternative to tag)
revision: int | None # Explicit revision number (used with agent_id)
message: str # User message to send (required)
thread_id: str | None # Thread ID for multi-turn conversations
model_id: str | None # Override the agent's default model
model_params: ModelParams | None # Override model parameters
end_user_id: str | None # End-user identifier for tracking
metadata: dict[str, Any] | None # Arbitrary metadata
Validation rules:
messageis required and cannot be empty- Either
tagoragentId/agent_idmust be provided, but not both tagmust match^[a-z0-9][a-z0-9\-_]*$(lowercase alphanumeric, hyphens, underscores; must start with a letter or digit)
ModelParams
- TypeScript
- Python
interface ModelParams {
temperature?: number; // 0–2 (inclusive)
maxTokens?: number; // Must be > 0
seed?: number;
}
class ModelParams:
temperature: float | None # 0–2 (inclusive)
max_tokens: int | None # Must be > 0
seed: int | None
Validation rules:
temperaturemust be between 0 and 2 (inclusive)maxTokens/max_tokensmust be a positive integer
AgentRunResult
- TypeScript
- Python
interface AgentRunResult {
threadId: string; // Thread ID for the conversation
traceId?: string; // Trace ID for observability
agentId: string; // Resolved agent ID
agentRevision: number; // Resolved agent revision
agentTag?: string; // Resolved agent tag (if applicable)
output: string; // Agent output text
toolCalls: AgentToolCall[]; // Tool calls made during execution
status: 'completed' | 'failed'; // Execution status
usage?: {
// Token usage stats
promptTokens?: number;
completionTokens?: number;
};
}
class AgentRunResult:
thread_id: str # Thread ID for the conversation
trace_id: str | None # Trace ID for observability
agent_id: str # Resolved agent ID
agent_revision: int # Resolved agent revision
agent_tag: str | None # Resolved agent tag (if applicable)
output: str # Agent output text
tool_calls: list[AgentToolCall] # Tool calls made during execution
status: 'completed' | 'failed' # Execution status
usage: UsageInfo | None # Token usage stats
UsageInfo
Token usage information for agent runs.
class UsageInfo:
prompt_tokens: int | None
completion_tokens: int | None
AgentToolCall
- TypeScript
- Python
interface AgentToolCall {
toolCallId: string;
name: string;
arguments: unknown;
result?: unknown;
}
class AgentToolCall:
tool_call_id: str
name: str
arguments: Any
result: Any | None
AgentStreamEvent
Union type of all possible streaming events:
- TypeScript
- Python
type AgentStreamEvent =
| AgentResolvedEvent
| OutputTextDeltaEvent
| ToolCallDeltaEvent
| ToolCallResultEvent
| SuggestionsEvent
| ChatCompletedEvent
| AgentErrorEvent
| CustomEvent;
AgentStreamEvent = (
AgentResolvedEvent
| OutputTextDeltaEvent
| ToolCallDeltaEvent
| ToolCallResultEvent
| SuggestionsEvent
| ChatCompletedEvent
| AgentErrorEvent
| CustomEvent
)
AgentResolvedEvent
Emitted once at stream start with resolved agent metadata.
- TypeScript
- Python
interface AgentResolvedEvent {
type: 'agent_resolved';
agentId: string;
agentRevision: number;
agentTag?: string;
threadId: string;
}
class AgentResolvedEvent:
type: 'agent_resolved'
agent_id: str
agent_revision: int
agent_tag: str | None
thread_id: str
OutputTextDeltaEvent
Streaming text chunk from the agent.
- TypeScript
- Python
interface OutputTextDeltaEvent {
type: 'output_text_delta';
delta: string;
}
class OutputTextDeltaEvent:
type: 'output_text_delta'
delta: str
ToolCallDeltaEvent
Streaming tool call argument chunk.
- TypeScript
- Python
interface ToolCallDeltaEvent {
type: 'tool_call_delta';
toolCallId: string;
name: string;
argumentsDelta: string;
}
class ToolCallDeltaEvent:
type: 'tool_call_delta'
tool_call_id: str
name: str
arguments_delta: str
ToolCallResultEvent
Completed tool call with result.
- TypeScript
- Python
interface ToolCallResultEvent {
type: 'tool_call_result';
toolCallId: string;
name: string;
arguments: unknown;
result: unknown;
}
class ToolCallResultEvent:
type: 'tool_call_result'
tool_call_id: str
name: str
arguments: Any
result: Any
SuggestionsEvent
Emitted when the agent provides follow-up suggestion prompts. Typically sent after the agent's response is complete. The SDK validates suggestions (max 6 items, label ≤ 40 chars) and silently drops invalid entries.
- TypeScript
- Python
interface SuggestionsEvent {
type: 'suggestions';
suggestions: Array<{
label: string; // Display text (max 40 chars)
message: string; // Message to send when clicked
}>;
}
class SuggestionsEvent:
type: 'suggestions'
suggestions: list[SuggestionItem]
class SuggestionItem:
label: str # Display text (max 40 chars)
message: str # Message to send when clicked
ChatCompletedEvent
Emitted when agent execution completes.
- TypeScript
- Python
interface ChatCompletedEvent {
type: 'chat_completed';
threadId: string;
status: 'completed' | 'failed';
output: string;
}
class ChatCompletedEvent:
type: 'chat_completed'
thread_id: str
status: 'completed' | 'failed'
output: str
AgentErrorEvent
Error event from the agent backend. Streaming stops after this event.
- TypeScript
- Python
interface AgentErrorEvent {
type: 'error';
code: string;
message: string;
}
class AgentErrorEvent:
type: 'error'
code: str
message: str
CustomEvent
Pass-through event for unknown SSE event names. Use this for domain-specific events that are not yet promoted to first-class SDK event types.
- TypeScript
- Python
interface CustomEvent {
type: 'custom';
event: string;
data: Record<string, unknown>;
}
class CustomEvent:
type: 'custom'
event: str
data: Any
See also: Client | Streaming Helpers | Errors