Add natural language map interaction to your CARTO + deck.gl application using AI-powered tool calling
Here you will learn how to set up an application where users interact with a CARTO + deck.gl map through natural language chat. The AI translates user messages into tool calls that manipulate the map — adding layers, navigating, placing markers, and applying spatial filters.
After completing this guide you will be familiar with the following concepts:
Installing and building @carto/agentic-deckgl
Configuring a backend server that connects to an AI model
Starting a frontend that renders the map and chat interface
Open the URL in your browser. You should see a map with a chat interface. Try typing a message like "Show me counties in California colored by population" to verify everything works.
Integrating in your own application
Once you've verified the example works, you can integrate the library into your own CARTO + deck.gl application. Install it from NPM:
Backend integration
On the server side, use the library to build the system prompt and convert tool definitions for your AI SDK:
Frontend integration
On the client side, use the response utilities to handle tool calls from the AI:
The tool calls generate deck.gl JSON specifications. Use JSONConverter from @deck.gl/json to resolve them into deck.gl layers and props.
cd examples/backend/openai-agents-sdk
npm install
cp .env.example .env
npm run dev
cd examples/frontend/react
pnpm install
cp .env.example .env
pnpm dev
npm install @carto/agentic-deckgl
import {
buildSystemPrompt,
getToolsForVercelAI,
TOOL_NAMES,
} from '@carto/agentic-deckgl';
// Build the system prompt with the current map state
const systemPrompt = buildSystemPrompt({
toolNames: [TOOL_NAMES.SET_DECK_STATE, TOOL_NAMES.SET_MARKER, TOOL_NAMES.SET_MASK_LAYER],
initialState: {
viewState: { latitude: 40.7128, longitude: -74.006, zoom: 12 },
layers: [],
},
});
// Get tool definitions in the format your AI SDK expects
const tools = getToolsForVercelAI();
import { parseToolResponse, isSuccessResponse } from '@carto/agentic-deckgl';
websocket.on('tool_call', async (message) => {
const result = await executeToolOnMap(message.tool, message.parameters);
const parsed = parseToolResponse(result);
if (isSuccessResponse(parsed)) {
console.log('Tool executed:', parsed.message);
}
});