---
title: "View source for AI Services/AI Chat - DreamFactory Wiki"
source: "http://wiki.dreamfactory.com/index.php?action=edit&title=AI_Services%2FAI_Chat"
canonical_url: "http://wiki.dreamfactory.com/index.php?action=edit&title=AI_Services%2FAI_Chat"
converted_at: "2026-11-23T19:12:13.964Z"
format: "markdown"
converted_by: "html-to-md-ai"
---
[]()
	
	
	
	# View source for AI Services/AI Chat

	
		
		← [AI Services/AI Chat](/AI_Services/AI_Chat)
		
		
		
		[Jump to navigation](#mw-head)
		[Jump to search](#searchInput)
		You do not have permission to edit this page, for the following reason:

The action you have requested is limited to users in the group: [Users](/index.php?title=DreamFactory_Wiki:Users&action=edit&redlink=1).---

You can view and copy the source of this page.

{{#seo:
|title=AI Chat - DreamFactory Documentation
|title_mode=replace
|description=Chat with your DreamFactory-governed data using AI and MCP tools. The AI queries databases, calls stored procedures, and accesses MCP services on your behalf.
|keywords=DreamFactory, AI Chat, chat with data, agentic AI, tool calling, MCP, database query, natural language query, session management
|canonical=https://wiki.dreamfactory.com/AI_Services/Ai_Chat
|og:title=AI Chat
|og:type=article
|og:site_name=DreamFactory Documentation
|og:description=Chat with your DreamFactory-governed data using AI and MCP tools. The AI queries databases, calls stored procedures, and accesses MCP services on your behalf.
}}
'''Chat with your DreamFactory-governed data using AI and MCP tools. The AI queries databases, calls stored procedures, and accesses MCP services on your behalf.'''

&lt;span id="ai-chat">&lt;/span>
== AI Chat ==

AI Chat lets users have natural-language conversations with their DreamFactory-governed data. Behind the scenes, an AI model runs an agentic tool-calling loop: it reads table schemas, queries data, calls stored procedures, and accesses MCP services, then synthesizes the results into a human-readable answer. All data access is governed by DreamFactory's standard role-based access control.

{{Note|AI Chat requires a DreamFactory commercial license (Gold or Platinum).}}

&lt;span id="how-it-works">&lt;/span>
== How It Works ==

# An admin creates an '''AI Chat''' service and links it to an '''AI Connection''' (the LLM provider) and a '''DreamFactory role''' (the data access scope).
# A user creates a '''chat session''' via the API.
# The user sends messages to the session. For each message, DreamFactory:
#* Passes the conversation history plus available tool definitions to the AI model.
#* If the AI wants to query data, it issues tool calls (get tables, get schema, query data, etc.). DreamFactory executes each tool call under the configured role's permissions and feeds the results back to the AI.
#* This loop continues until the AI produces a final text response or hits the tool-call limit.
# The response (with token counts and tool-call metadata) is returned to the user.

&lt;span id="tool-surface">&lt;/span>
=== Tool Surface ===

The AI Chat service automatically discovers which tools to expose based on the role's service access grants:

* '''Database services''' (SQL, MongoDB, Snowflake, etc.): the AI gets &lt;code>get_tables&lt;/code>, &lt;code>get_table_schema&lt;/code>, &lt;code>get_table_data&lt;/code>, &lt;code>get_table_fields&lt;/code>, and &lt;code>get_table_relationships&lt;/code> tools for each accessible database service.
* '''MCP services''': the AI discovers available MCP tools at runtime via JSON-RPC &lt;code>tools/list&lt;/code> and can invoke them during the conversation.
* '''Excluded services''': AI Connection, AI Chat, System, User, Swagger, and API Docs services are never exposed to the AI.

Tool names are prefixed with the service name so the AI can work across multiple databases in a single conversation. For example, a database service named &lt;code>hr_db&lt;/code> produces tools like &lt;code>hr_db__get_tables&lt;/code>, &lt;code>hr_db__get_table_data&lt;/code>, etc.

&lt;span id="creating-an-ai-chat-service">&lt;/span>
== Creating an AI Chat Service ==

# In the DreamFactory admin panel, go to '''Services > Create'''.
# Select '''AI Chat''' from the service type list.
# Configure the required fields:

&lt;span id="configuration-fields">&lt;/span>
=== Configuration Fields ===

{| class="wikitable"
|-
! '''Field'''
! '''Required'''
! '''Description'''
|-
| '''AI Connection'''
| Yes
| The AI Connection service (Anthropic, OpenAI, etc.) that powers this chat. The connection's system prompt, allowed models, and rate limits all still apply.
|-
| '''AI Role (data access)'''
| Yes
| The DreamFactory role the AI operates under for tool calls. Use a least-privilege role scoped to the data you want the AI to read. This is the only access bottleneck for tool calls.
|-
| '''Default data services'''
| No
| JSON array of service names the AI can access. Acts as a whitelist on top of the role's permissions (the AI sees the intersection). Leave blank to expose every service the role can access. Example: &lt;code>["dellstore_db","hr_db"]&lt;/code>
|-
| '''Default system prompt'''
| No
| Instructions injected at the top of every chat session. Sets the assistant's persona, restricts topics, or describes the data it should query.
|-
| '''Max tool calls per turn'''
| No
| Hard cap on tool calls in a single user turn before the AI must respond. Default: 25. Increase for complex multi-step workflows.
|-
| '''Max messages per session'''
| No
| Hard cap on session length. Protects against runaway context costs. Default: 200.
|}

&lt;span id="security-model">&lt;/span>
=== Security Model ===

The AI role must appear in the linked AI Connection's '''Allowed Roles''' list. This is validated at session creation time. The actual data access enforcement happens at the wire level on every tool call: the orchestrator sends each request with the AI agent's JWT and the role-bound API key, and DreamFactory's standard RBAC pipeline gates the request server-side.

This means:

* Adding a database service to the role's access list makes it available to the AI.
* Removing it removes it. No parallel configuration is needed.
* If the role loses access between session creation and a tool call, the call returns a 403 which is surfaced to the AI as a tool error.

&lt;span id="api-endpoints">&lt;/span>
== API Endpoints ==

Assuming the AI Chat service is named &lt;code>my_chat&lt;/code>:

&lt;span id="create-a-session">&lt;/span>
=== Create a Session ===

'''&lt;code>POST /api/v2/{service-name}/session&lt;/code>'''

Creates a new chat session.

'''Request:'''

&lt;syntaxhighlight lang="json">{
  "data_services": ["dellstore_db", "hr_db"],
  "allowed_resources": {
    "dellstore_db": ["customers", "orders"]
  },
  "title": "Q4 Sales Analysis",
  "system_prompt": "You are a data analyst. Always show your SQL reasoning."
}&lt;/syntaxhighlight>

{| class="wikitable"
|-
! '''Field'''
! '''Required'''
! '''Description'''
|-
| &lt;code>data_services&lt;/code>
| No
| Override: limit the AI to these services for this session. Empty = use everything the role allows.
|-
| &lt;code>allowed_resources&lt;/code>
| No
| Override: per-service table restrictions.
|-
| &lt;code>title&lt;/code>
| No
| Session title. Auto-set to the first message if omitted.
|-
| &lt;code>system_prompt&lt;/code>
| No
| Override the service's default system prompt for this session.
|}

'''Response:'''

&lt;syntaxhighlight lang="json">{
  "id": 42,
  "service_id": 5,
  "user_id": 1,
  "data_services": ["dellstore_db", "hr_db"],
  "title": "Q4 Sales Analysis",
  "status": "active",
  "total_input_tokens": 0,
  "total_output_tokens": 0,
  "tool_call_count": 0,
  "created_at": "2026-06-01T10:00:00.000000Z",
  "updated_at": "2026-06-01T10:00:00.000000Z"
}&lt;/syntaxhighlight>

&lt;span id="send-a-message">&lt;/span>
=== Send a Message ===

'''&lt;code>POST /api/v2/{service-name}/session/{session_id}&lt;/code>'''

Send a user message and receive the AI's response.

'''Request:'''

&lt;syntaxhighlight lang="json">{
  "message": "What are the top 10 customers by total order value?"
}&lt;/syntaxhighlight>

'''Response:'''

&lt;syntaxhighlight lang="json">{
  "content": "Here are the top 10 customers by total order value:\n\n| Customer | Total Orders |\n|...",
  "input_tokens": 2450,
  "output_tokens": 380,
  "tool_calls_made": 3,
  "finish_reason": "stop"
}&lt;/syntaxhighlight>

The &lt;code>tool_calls_made&lt;/code> field tells you how many database queries or MCP tool calls the AI executed to produce this answer.

&lt;span id="list-sessions">&lt;/span>
=== List Sessions ===

'''&lt;code>GET /api/v2/{service-name}/session&lt;/code>'''

Returns all sessions for the current user. Admins see all sessions.

'''Query parameters:'''

{| class="wikitable"
|-
! '''Parameter'''
! '''Default'''
! '''Description'''
|-
| &lt;code>status&lt;/code>
| &lt;code>active&lt;/code>
| Filter by status: &lt;code>active&lt;/code>, &lt;code>archived&lt;/code>, or &lt;code>all&lt;/code>.
|}

&lt;span id="get-session-detail">&lt;/span>
=== Get Session Detail ===

'''&lt;code>GET /api/v2/{service-name}/session/{session_id}&lt;/code>'''

Returns the session with its message history.

'''Query parameters:'''

{| class="wikitable"
|-
! '''Parameter'''
! '''Default'''
! '''Description'''
|-
| &lt;code>message_limit&lt;/code>
| 50
| Max messages to return (most recent first, reversed to chronological order). Capped at 500.
|}

The response includes a &lt;code>messages&lt;/code> array where each message has:

{| class="wikitable"
|-
! '''Field'''
! '''Description'''
|-
| &lt;code>role&lt;/code>
| &lt;code>user&lt;/code>, &lt;code>assistant&lt;/code>, &lt;code>tool&lt;/code>, or &lt;code>system&lt;/code>
|-
| &lt;code>content&lt;/code>
| Message text or tool result
|-
| &lt;code>tool_calls&lt;/code>
| Array of tool calls (for assistant messages that triggered tools)
|-
| &lt;code>tool_name&lt;/code>
| Name of the tool called (for tool-result messages)
|-
| &lt;code>is_error&lt;/code>
| Whether this tool result was an error
|-
| &lt;code>input_tokens&lt;/code>
| Tokens consumed by this turn
|-
| &lt;code>output_tokens&lt;/code>
| Tokens generated by this turn
|-
| &lt;code>latency_ms&lt;/code>
| Response time for this turn
|}

&lt;span id="delete-a-session">&lt;/span>
=== Delete a Session ===

'''&lt;code>DELETE /api/v2/{service-name}/session/{session_id}&lt;/code>'''

Deletes the session and all its messages. Non-admins can only delete their own sessions.

&lt;span id="ai-chat-vs-data-chat">&lt;/span>
== AI Chat vs. Data Chat ==

DreamFactory provides two ways to chat with your data:

{| class="wikitable"
|-
! '''Feature'''
! '''AI Chat (df-ai-chat)'''
! '''Data Chat (df-ai, built-in)'''
|-
| '''Service type'''
| Separate &lt;code>ai_chat&lt;/code> service
| Resource on &lt;code>ai_connection&lt;/code> service (&lt;code>/data-chat&lt;/code>)
|-
| '''Sessions'''
| Persistent sessions with message history
| Stateless (caller manages conversation)
|-
| '''MCP tools'''
| Yes. Discovers and invokes MCP service tools
| No
|-
| '''Access control'''
| Dedicated AI role. Role-derived tool surface.
| App-based (Data Access API Key)
|-
| '''Tool-call tracking'''
| Per-session counters (total_input_tokens, tool_call_count)
| Returned inline per request
|-
| '''Usage logging'''
| Writes to &lt;code>ai_usage_log&lt;/code> with &lt;code>resource=chat-session&lt;/code>
| Writes to &lt;code>ai_usage_log&lt;/code> with &lt;code>resource=data-chat&lt;/code>
|}

Use '''AI Chat''' when you need persistent sessions, MCP integration, or fine-grained role-based scoping. Use '''Data Chat''' for quick stateless queries where the caller manages conversation state.

&lt;span id="usage-logging-and-audit">&lt;/span>
== Usage Logging and Audit ==

Every AI provider call from the orchestrator is logged to &lt;code>ai_usage_log&lt;/code> with &lt;code>resource=chat-session&lt;/code>, distinct from direct REST gateway calls (&lt;code>resource=chat&lt;/code>). Both contribute to the same AI Connection's cost attribution and monthly budget calculations.

Prompt logging and SIEM dispatch also apply to AI Chat sessions when configured on the underlying AI Connection. See [[AI_Services/Ai_Gateway|AI Gateway Analytics]] for details.

&lt;span id="configuration-limits">&lt;/span>
== Configuration Limits ==

{| class="wikitable"
|-
! '''Setting'''
! '''Default'''
! '''Config Key'''
|-
| Max tool calls per message
| 25
| &lt;code>ai-chat.max_tool_calls_per_message&lt;/code>
|-
| Max messages per session
| 200
| &lt;code>ai-chat.max_messages_per_session&lt;/code>
|-
| Tool result max length
| 50,000 chars
| &lt;code>ai-chat.tool_result_max_length&lt;/code>
|-
| Message limit max (API)
| 500
| &lt;code>ai-chat.message_limit_max&lt;/code>
|}

When a tool result exceeds the max length, it is truncated with a &lt;code>[TRUNCATED]&lt;/code> marker instructing the AI to use filters or limits to narrow the query.

== See also ==
* [[AI_Services/Ai_Connection|AI Connection]]
* [[AI_Services/Ai_Gateway|AI Gateway Analytics]]
* [[AI_Services/Mcp_Service|MCP Server]]

[[Category:Ai_Chat]]
[[Category:Ai_Integration]]
[[Category:Agentic_Ai]]
[[Category:Difficulty_Intermediate]]
Template used on this page:

- [Template:Note](/Template:Note) ([view source](/index.php?title=Template:Note&action=edit))

Return to [AI Services/AI Chat](/AI_Services/AI_Chat).

Retrieved from "[https://wiki.dreamfactory.com/AI_Services/AI_Chat](https://wiki.dreamfactory.com/AI_Services/AI_Chat)"