Cortex Agent¶
cortex_agents.agent.CortexAgent ¶
CortexAgent(account_url: str | None = None, pat: str | None = None, enable_logging: bool = True, token_type: str | None = None)
Bases: BaseAgent
Client for Snowflake Cortex Agents.
Provides a simple, ergonomic interface for managing agents, running agents with streaming support, managing conversation threads, collecting user feedback, and parsing responses.
Examples:
# Use with context manager for automatic cleanup
with CortexAgent(account_url="https://...", pat="token123") as client:
# Create an agent
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 with streaming
response = client.run(
"What's the revenue?",
agent_name="MY_AGENT",
database="MY_DB",
schema="MY_SCHEMA"
)
# Stream events in real-time
for event in response.stream():
if event["type"] == "text.delta":
print(event["data"]["text"], end="", flush=True)
# Or access complete results after streaming
print(response.text)
print(response.sql)
# Submit feedback
client.submit_feedback(
agent_name="MY_AGENT",
positive=True,
orig_request_id=response.request_id,
database="MY_DB",
schema="MY_SCHEMA"
)
Initialize the Cortex Agent client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account_url
|
str | None
|
Snowflake account URL. Defaults to SNOWFLAKE_ACCOUNT_URL environment variable. |
None
|
pat
|
str | None
|
Personal access token. Defaults to SNOWFLAKE_PAT environment variable. |
None
|
enable_logging
|
bool
|
Enable request/response logging. Defaults to True. |
True
|
token_type
|
str | None
|
Authorization token type (e.g. |
None
|
Source code in cortex_agents/agent.py
close ¶
create_agent ¶
create_agent(name: str, config: dict[str, Any], database: str, schema: str, create_mode: str | None = None) -> dict[str, Any]
Create a Cortex Agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name |
required |
config
|
dict[str, Any]
|
Agent configuration as dict (instructions, models, tools, etc.) |
required |
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
create_mode
|
str | None
|
Optional creation mode |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Response with status |
Examples:
client.create_agent(
name="MY_AGENT",
config={
"comment": "My helpful agent",
"instructions": {
"system": "You are a helpful assistant",
"response": "Be concise"
},
"models": {"orchestration": "claude-sonnet-4-6"},
"tools": [{
"tool_spec": {
"type": "cortex_analyst_text2sql",
"name": "analyst"
}
}],
"tool_resources": {
"analyst": {
"semantic_view": "DB.SCHEMA.VIEW",
"execution_environment": {
"type": "warehouse",
"warehouse": "MY_WH"
}
}
}
},
database="MY_DB",
schema="MY_SCHEMA"
)
Source code in cortex_agents/agent.py
get_agent ¶
Get agent details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name |
required |
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Agent configuration and metadata |
Source code in cortex_agents/agent.py
update_agent ¶
Update an existing agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name |
required |
config
|
dict[str, Any]
|
Updated configuration (partial updates allowed) |
required |
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Response with status |
Source code in cortex_agents/agent.py
list_agents ¶
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 a schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
like
|
str | None
|
Filter pattern (SQL wildcards) |
None
|
from_name
|
str | None
|
Start listing after this name |
None
|
limit
|
int | None
|
Maximum number to return (1-10000) |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
list[Dict]: List of agent metadata |
Source code in cortex_agents/agent.py
delete_agent ¶
Delete an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Agent name |
required |
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
if_exists
|
bool
|
Don't error if agent doesn't exist |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Response with status |
Source code in cortex_agents/agent.py
run ¶
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
Run an agent with a query.
This is a unified method that works both with saved agents and inline configuration. Always returns a streaming response (SSE events).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str | None
|
The user's current question. Optional when |
None
|
agent_name
|
str | None
|
Name of saved agent. Required if using a saved agent. |
None
|
database
|
str | None
|
Database name. Required if agent_name is provided. |
None
|
schema
|
str | None
|
Schema name. Required if agent_name is provided. |
None
|
thread_id
|
str | int | None
|
Thread identifier for conversation continuity. |
None
|
parent_message_id
|
str | int | None
|
Parent message ID. Required if thread_id is set. |
None
|
tool_choice
|
dict[str, Any] | None
|
Controls tool selection strategy. Can be:
- |
None
|
messages
|
list[dict[str, Any]] | None
|
Conversation history to continue from. When provided, the
latest user question from |
None
|
agent_config
|
AgentInlineConfig | None
|
Inline agent configuration for ephemeral runs.
Only used if |
None
|
Returns:
| Type | Description |
|---|---|
AgentResponse
|
A response object with streaming events. |
AgentResponse
|
|
AgentResponse
|
|
Examples:
# Stream events in real-time with a saved agent
with CortexAgent(account_url="...", pat="...") as client:
response = client.run(
"What's the revenue?",
agent_name="MY_AGENT",
database="SALES_DB",
schema="ANALYTICS"
)
# Stream events
for event in response.stream():
if event["type"] == "text.delta":
print(event["data"]["text"], end="", flush=True)
# Access complete results after streaming
print(response.text)
print(response.sql)
# With tool choice constraints
response = client.run(
"Analyze this",
agent_name="MY_AGENT",
database="DB",
schema="SCHEMA",
tool_choice={"type": "required"} # Require at least one tool
)
# Use only specific tools
response = client.run(
"Check inventory",
agent_name="MY_AGENT",
database="DB",
schema="SCHEMA",
tool_choice={
"type": "tool",
"name": ["analyst_tool", "search_tool"]
}
)
# Inline agent config (no database/schema needed)
response = client.run(
"What's the revenue?",
agent_config={
"models": {"orchestration": "claude-sonnet-4-6"},
"instructions": {"system": "You are helpful"},
"tools": [...],
"tool_resources": {...},
}
)
# In a conversation thread
response = client.run(
"Continue analysis",
agent_name="MY_AGENT",
database="DB",
schema="SCHEMA",
thread_id=thread_id,
parent_message_id=last_message_id
)
Source code in cortex_agents/agent.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
create_thread ¶
Create a 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]
|
Dictionary containing thread metadata including thread_id, thread_name, |
dict[str, Any]
|
origin_application, created_on (datetime), and updated_on (datetime). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If origin_app exceeds 16 bytes. |
Examples:
thread = client.create_thread(origin_app="my_app")
thread_id = thread["thread_id"]
created = thread["created_on"] # datetime object
Source code in cortex_agents/agent.py
get_thread ¶
get_thread(thread_id: str | int, limit: int = 20, last_message_id: int | None = None) -> dict[str, Any]
Get thread details and messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Thread ID |
required |
limit
|
int
|
Number of messages to return (max 100) |
20
|
last_message_id
|
int | None
|
Last message ID for pagination |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Contains "metadata" and "messages" arrays |
Source code in cortex_agents/agent.py
update_thread ¶
Update thread name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Thread ID |
required |
name
|
str
|
New thread name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Response with status |
Source code in cortex_agents/agent.py
list_threads ¶
List all threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origin_app
|
str | None
|
Filter by application name |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of thread dictionaries, each containing thread_id, thread_name, |
list[dict[str, Any]]
|
origin_application, created_on (datetime), and updated_on (datetime). |
Examples:
threads = client.list_threads(origin_app="my_app")
for thread in threads:
print(f"{thread['thread_id']}: {thread['thread_name']}")
print(f"Created: {thread['created_on']}")
Source code in cortex_agents/agent.py
delete_thread ¶
Delete a thread and all its messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thread_id
|
str | int
|
Thread ID |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Success response |
Source code in cortex_agents/agent.py
submit_feedback ¶
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 or a specific agent response.
Can submit feedback at two levels: - Agent-level: General feedback about the agent (don't provide orig_request_id) - Request-level: Feedback about a specific response (provide orig_request_id)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_name
|
str
|
Name of the agent |
required |
database
|
str
|
Database name |
required |
schema
|
str
|
Schema name |
required |
positive
|
bool
|
Whether the feedback is positive (True) or negative (False) |
required |
feedback_message
|
str | None
|
Optional detailed feedback message |
None
|
categories
|
list[str] | None
|
Optional list of feedback categories (e.g., ["Something worked well"]) |
None
|
orig_request_id
|
str | None
|
Optional request ID from response.request_id for request-level feedback |
None
|
thread_id
|
str | int | None
|
Optional thread ID if feedback is for a threaded conversation |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Confirmation response |
Examples:
# Agent-level feedback
client.submit_feedback(
agent_name="MY_AGENT",
database="DB",
schema="SCHEMA",
positive=True,
feedback_message="Great agent!",
categories=["Something worked well"]
)
# Request-level feedback
response = client.run("query", agent_name="MY_AGENT", database="DB", schema="SCHEMA")
client.submit_feedback(
agent_name="MY_AGENT",
database="DB",
schema="SCHEMA",
positive=True,
orig_request_id=response.request_id,
feedback_message="Perfect answer!"
)
Source code in cortex_agents/agent.py
list_models ¶
List available Cortex models.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
dict[str, Any]
|
Available models and their configurations |