Async Cortex Agent¶
cortex_agents.async_agent.AsyncCortexAgent ¶
AsyncCortexAgent(account_url: str | None = None, pat: str | None = None, enable_logging: bool = True, token_type: str | None = None)
Bases: BaseAgent
Async client for Snowflake Cortex Agents.
Exposes ergonomic helpers for managing agents, executing runs with streaming responses, orchestrating conversation threads, and collecting feedback. Use the async context manager for automatic resource management.
Examples:
# Use with async context manager for automatic cleanup
async with AsyncCortexAgent(account_url="https://...", pat="token123") as client:
# Create an agent
await client.create_agent(
name="MY_AGENT",
config={
"instructions": {"system": "You are helpful"},
"models": {"orchestration": "claude-sonnet-4-6"}
},
database="MY_DB",
schema="MY_SCHEMA"
)
# Run the agent
response = await client.run(
"What's the revenue?",
agent_name="MY_AGENT",
database="MY_DB",
schema="MY_SCHEMA"
)
# Stream results
async for event in response.astream():
if event['type'] == 'text.delta':
print(event['data']['text'], end='', flush=True)
# Submit feedback
await client.submit_feedback(
agent_name="MY_AGENT",
database="MY_DB",
schema="MY_SCHEMA",
positive=True,
orig_request_id=response.request_id
)
Initialize the async Cortex agent facade and lazy managers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_url
|
str | None
|
Optional Snowflake account URL; falls back to environment variable. |
None
|
pat
|
str | None
|
Optional personal access token; falls back to environment variable. |
None
|
enable_logging
|
bool
|
Whether to emit debug log entries for HTTP activity. |
True
|
token_type
|
str | None
|
Authorization token type (e.g. |
None
|
Source code in cortex_agents/async_agent.py
close ¶
Synchronously close resources for compatibility with BaseAgent.
Source code in cortex_agents/async_agent.py
aclose
async
¶
Close the httpx client and drop cached managers for reuse.
Source code in cortex_agents/async_agent.py
create_agent
async
¶
create_agent(name: str, config: dict[str, Any], database: str, schema: str, create_mode: str | None = None) -> dict[str, Any]
Create a Cortex agent with the provided configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical name for the agent within the schema. |
required |
config
|
dict[str, Any]
|
Configuration payload including instructions, models, tools, etc. |
required |
database
|
str
|
Target database for the agent. |
required |
schema
|
str
|
Target schema for the agent. |
required |
create_mode
|
str | None
|
Optional behaviour flag (for example, "CREATE OR REPLACE"). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Raw Snowflake API response describing the new agent. |
Source code in cortex_agents/async_agent.py
get_agent
async
¶
Retrieve a single agent's metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name to fetch. |
required |
database
|
str
|
Database containing the agent. |
required |
schema
|
str
|
Schema containing the agent. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Agent definition and metadata. |
Source code in cortex_agents/async_agent.py
update_agent
async
¶
Update an existing agent with a partial or full configuration payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name to modify. |
required |
config
|
dict[str, Any]
|
Partial or full configuration update. |
required |
database
|
str
|
Database containing the agent. |
required |
schema
|
str
|
Schema containing the agent. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Raw Snowflake API response for the update request. |
Source code in cortex_agents/async_agent.py
list_agents
async
¶
list_agents(database: str, schema: str, like: str | None = None, from_name: str | None = None, limit: int | None = None) -> list[dict[str, Any]]
List agents in the specified schema with optional pagination and filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
str
|
Database containing the agents. |
required |
schema
|
str
|
Schema containing the agents. |
required |
like
|
str | None
|
Optional SQL-style filter pattern. |
None
|
from_name
|
str | None
|
Continue listing after this agent name. |
None
|
limit
|
int | None
|
Maximum number of results to return. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: Collection of agent metadata dictionaries. |
Source code in cortex_agents/async_agent.py
delete_agent
async
¶
Delete a Cortex agent, optionally ignoring missing agents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name to delete. |
required |
database
|
str
|
Database containing the agent. |
required |
schema
|
str
|
Schema containing the agent. |
required |
if_exists
|
bool
|
Do not raise an error when the agent is absent. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: API response confirming the operation. |
Source code in cortex_agents/async_agent.py
run
async
¶
run(query: str | None = None, agent_name: str | None = None, database: str | None = None, schema: str | None = None, thread_id: str | int | None = None, parent_message_id: str | int | None = None, tool_choice: dict[str, Any] | None = None, messages: list[dict[str, Any]] | None = None, stream: bool = True, agent_config: AgentInlineConfig | None = None) -> AgentResponse
Execute an agent run and return an async response wrapper.
The method supports saved agents, inline configurations, and conversational
context via threads. The returned AgentResponse exposes streaming
helpers for consuming Server-Sent Events (SSE) asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str | None
|
User question to send to the agent. |
None
|
agent_name
|
str | None
|
Saved agent name (required when |
None
|
database
|
str | None
|
Database containing the saved agent. |
None
|
schema
|
str | None
|
Schema containing the saved agent. |
None
|
thread_id
|
str | int | None
|
Optional conversation thread identifier. |
None
|
parent_message_id
|
str | int | None
|
Optional parent message id used with threads. |
None
|
tool_choice
|
dict[str, Any] | None
|
Tool execution strategy override. |
None
|
messages
|
list[dict[str, Any]] | None
|
Conversation history to include in the request payload. |
None
|
agent_config
|
AgentInlineConfig | None
|
Inline configuration for ad-hoc runs. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AgentResponse |
AgentResponse
|
Wrapper offering async iteration over streaming events. |
Examples:
async with AsyncCortexAgent(account_url="...", pat="...") as client:
# With saved agent
response = await client.run(
"What's the revenue?",
agent_name="MY_AGENT",
database="SALES_DB",
schema="ANALYTICS"
)
# Stream results
async for event in response.astream():
if event['type'] == 'text.delta':
print(event['data']['text'], end='', flush=True)
Source code in cortex_agents/async_agent.py
create_thread
async
¶
Create a new Cortex conversation thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin_app
|
str | None
|
Optional application identifier (max 16 bytes). Example: "my_app" or "sales_bot" |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Thread metadata including the thread identifier. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If origin_app exceeds 16 bytes. |
Source code in cortex_agents/async_agent.py
get_thread
async
¶
get_thread(thread_id: str | int, *, limit: int = 20, last_message_id: int | None = None) -> dict[str, Any]
Fetch thread metadata and messages with optional pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Thread identifier to fetch. |
required |
limit
|
int
|
Number of messages to return (max 100). |
20
|
last_message_id
|
int | None
|
Continue listing messages after this id. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Metadata and message history for the thread. |
Source code in cortex_agents/async_agent.py
update_thread
async
¶
Rename an existing conversation thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Identifier for the thread to update. |
required |
name
|
str
|
New display name for the thread. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: API response describing the updated thread. |
Source code in cortex_agents/async_agent.py
list_threads
async
¶
List available threads, optionally filtered by origin application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin_app
|
str | None
|
Optional application identifier to filter the results. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: Collection of thread metadata entries. |
Source code in cortex_agents/async_agent.py
delete_thread
async
¶
Delete a conversation thread and its messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Identifier for the thread to remove. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: API response confirming deletion. |
Source code in cortex_agents/async_agent.py
submit_feedback
async
¶
submit_feedback(agent_name: str, database: str, schema: str, *, positive: bool, feedback_message: str | None = None, categories: list[str] | None = None, orig_request_id: str | None = None, thread_id: str | int | None = None) -> dict[str, Any]
Submit feedback about an agent response or interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_name
|
str
|
Agent associated with the feedback entry. |
required |
database
|
str
|
Database containing the agent. |
required |
schema
|
str
|
Schema containing the agent. |
required |
positive
|
bool
|
Indicates positive (True) or negative (False) feedback. |
required |
feedback_message
|
str | None
|
Optional free-form feedback text. |
None
|
categories
|
list[str] | None
|
Optional list of structured feedback categories. |
None
|
orig_request_id
|
str | None
|
Optional request identifier from a previous run. |
None
|
thread_id
|
str | int | None
|
Optional conversation thread to associate with feedback. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: API acknowledgement payload. |
Source code in cortex_agents/async_agent.py
list_models
async
¶
Return the list of Cortex models visible to the current account.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Mapping of available models and their attributes. |