Chat with an agent by external id
- async AsyncCogniteClient.agents.chat(
- agent_external_id: str,
- messages: Message | ActionResult | Sequence[Message | ActionResult],
- cursor: str | None = None,
- actions: Sequence[Action] | None = None,
- retention_policy: Literal['temporary', 'persisted'] | None = None,
- tool_confirmation_mode: Literal['default', 'dangerouslyAllowAll'] | None = None,
-
Given a user query, the Atlas AI agent responds by reasoning and using the tools associated with it. Users can ensure conversation continuity by including the cursor from the previous response in subsequent requests.
- Parameters:
agent_external_id (str) – External ID that uniquely identifies the agent.
messages (Message | ActionResult | Sequence[Message | ActionResult]) – A list of one or many input messages to the agent. Can include regular messages and action results.
cursor (str | None) – The cursor to use for continuation of a conversation. Use this to create multi-turn conversations, as the cursor will keep track of the conversation state.
actions (Sequence[Action] | None) – A list of client-side actions that can be called by the agent.
retention_policy (Literal['temporary', 'persisted'] | None) – Retention class for the conversation. “persisted” retains the conversation until explicitly deleted. “temporary” (the API default) can result in the conversation being deleted at any time. Only honored when starting a conversation, ignored when a cursor is given.
tool_confirmation_mode (Literal['default', 'dangerouslyAllowAll'] | None) – Controls whether tools that require user confirmation prompt for approval. “default” (the API default) enforces the standard confirmation requirements, while “dangerouslyAllowAll” bypasses approval for tools executed during this request, including in subagents. Not persisted across turns, so it must be passed with each request.
- Returns:
The response from the agent.
- Return type:
Examples
Start a simple chat with an agent:
>>> from cognite.client import CogniteClient >>> from cognite.client.data_classes.agents import Message >>> client = CogniteClient() >>> # async_client = AsyncCogniteClient() # another option >>> response = client.agents.chat( ... agent_external_id="my_agent", messages=Message("What can you help me with?") ... ) >>> print(response.text)
Continue a conversation using the cursor:
>>> follow_up = client.agents.chat( ... agent_external_id="my_agent", ... messages=Message("Tell me more about that"), ... cursor=response.cursor, ... )
Start a conversation that is retained until explicitly deleted:
>>> response = client.agents.chat( ... agent_external_id="my_agent", ... messages=Message("What can you help me with?"), ... retention_policy="persisted", ... )
Send multiple messages at once:
>>> response = client.agents.chat( ... agent_external_id="my_agent", ... messages=[ ... Message("Help me find the 1st stage compressor."), ... Message("Once you have found it, find related time series."), ... ], ... )
Chat with client-side actions:
>>> from cognite.client.data_classes.agents import ClientToolAction, ClientToolResult >>> add_numbers_action = ClientToolAction( ... name="add", ... description="Add two numbers together", ... parameters={ ... "type": "object", ... "properties": { ... "a": {"type": "number", "description": "First number"}, ... "b": {"type": "number", "description": "Second number"}, ... }, ... "required": ["a", "b"], ... }, ... ) >>> response = client.agents.chat( ... agent_external_id="my_agent", ... messages=Message("What is 42 plus 58?"), ... actions=[add_numbers_action], ... ) >>> if response.action_calls: ... for call in response.action_calls: ... # Execute the action ... result = call.arguments["a"] + call.arguments["b"] ... # Send result back ... response = client.agents.chat( ... agent_external_id="my_agent", ... messages=ClientToolResult( ... action_id=call.action_id, content=f"The result is {result}" ... ), ... cursor=response.cursor, ... actions=[add_numbers_action], ... )
Handle tool confirmation for integration tools (Call Function, Run Python code, Call REST API):
>>> from cognite.client.data_classes.agents import ( ... ToolConfirmationCall, ... ToolConfirmationResult, ... ) >>> response = client.agents.chat( ... agent_external_id="my_agent", ... messages=Message("Run the data quality check function"), ... ) >>> if response.action_calls: ... confirmations = [] ... for action in response.action_calls: ... if isinstance(action, ToolConfirmationCall): ... # Inspect each tool call before deciding ... print(f"Tool: {action.tool_name}, type: {action.tool_type}") ... print(f"Arguments: {action.tool_arguments}") ... confirmations.append( ... ToolConfirmationResult( ... action_id=action.action_id, ... status="ALLOW", # or "DENY" to cancel ... ) ... ) ... if confirmations: ... response = client.agents.chat( ... agent_external_id="my_agent", ... messages=confirmations, ... cursor=response.cursor, ... )
Handle a user session request, letting the agent run a tool on your behalf:
>>> from cognite.client.data_classes.agents import UserSessionCall, UserSessionResult >>> if response.action_calls: ... session_results = [] ... for action in response.action_calls: ... if isinstance(action, UserSessionCall): ... print(f"Tool: {action.tool_name}, arguments: {action.tool_arguments}") ... # Always use one-shot token exchange here to avoid accumulating sessions ... # and exhausting the project's session limit: ... session = client.iam.sessions.create( ... session_type="ONESHOT_TOKEN_EXCHANGE" ... ) ... session_results.append( ... UserSessionResult(action_id=action.action_id, nonce=session.nonce) ... ) ... if session_results: ... response = client.agents.chat( ... agent_external_id="my_agent", ... messages=session_results, ... cursor=response.cursor, ... )