Agent-to-agent: using the Chat API as a tool
In this recipe, you will learn how to wrap the Cube Chat API as a tool for an external AI agent, enabling agent-to-agent analytics workflows.Use case
When building AI-powered applications, you often have an orchestrating agent (built with frameworks like LangChain, LlamaIndex, or CrewAI) that handles user conversations and coordinates multiple capabilities. One of these capabilities might be answering data questions — revenue trends, customer metrics, pipeline analysis, and so on. Rather than building a custom data retrieval pipeline, you can give your agent a tool that calls the Cube Chat API. This way, the Cube AI agent handles the hard parts — understanding the data model, writing correct queries, and summarizing results — while your orchestrating agent decides when to ask data questions and how to fold the answers into its broader workflow.Architecture
The following diagram shows how the orchestrating agent delegates data questions to the Cube AI agent via the Chat API: Key benefits of this approach:- Separation of concerns. Your agent handles conversation flow and business logic; Cube handles data access, governance, and query optimization.
- Built-in security. Row-level security, data access policies, and user attributes are enforced by the Cube layer — your agent does not need to implement them.
- Multi-turn context. By reusing a
chatId, the Cube agent retains conversational context, so follow-up questions like “now break that down by region” work automatically.
Prerequisites
Before you begin, make sure you have:- A Cube Cloud deployment on a Premium or Enterprise plan
- An AI agent configured in Admin -> Agents
- An API key with access to the agent
- The Chat API URL copied from your agent settings
Implementation
Wrapping the Chat API as a tool
The core idea is to write a function that sends a question to the Cube Chat API, collects the streamed response, and returns the final answer as a plain string. You then register this function as a tool that your agent can invoke. Here is a helper that calls the Chat API and extracts the final answer:The function filters streamed messages for those where
graphPath[0] === "final" to get the consolidated answer. See the
Chat API reference for details on the response format.LangChain integration
Below is a complete example of a LangChain agent that has access to the Cube Chat API as a tool. When the agent decides it needs data to answer a question, it calls theask_cube tool automatically.
- Read the user’s request and decide it needs data.
- Call
ask_cubewith a focused data question (e.g., “What was total revenue last quarter?”). - Receive the Cube agent’s answer with queried data and analysis.
- Optionally call
ask_cubeagain for additional data points. - Compose the final executive summary using all collected data.
Passing user context
If your application has per-user data access policies, pass the current user’s identity and attributes throughsessionSettings so that
the Cube agent enforces row-level security:
Multi-turn conversations
To maintain context across multiple questions in a single workflow, reuse thechatId returned by the Cube Chat API: