Query Events API Reference
The Query Events API enables logging and querying tool retrieval events for dashboard visibility and observability.Overview
Purpose
- Log tool retrieval queries with metadata (selected tools, scores, source)
- Retrieve historical queries for analytics and debugging
- Track request origin (CLI, API, Dashboard)
- Monitor tool selection patterns and performance
Base URL
Endpoints
/v2/retrieve_tools — Tool Retrieval (with Logging)
Automatically logs to query_events when user is authenticated.
Endpoint: GET /v2/retrieve_tools
Description: Retrieve tools matching a query. Logs the request and results to query_events table automatically.
Request
Headers:
Response
Status: 200 OKSide Effects
When authenticated, automatically logs toquery_events table:
Examples
CLI Request (with MCP header):source: "mcp".
API Request (no special header):
source: "api" (default).
Dashboard Request:
source: "dashboard".
QueryEventService (Backend)
TheQueryEventService is the modular service responsible for logging queries.
Import
Methods
__init__(supabase_url: Optional[str] = None, service_key: Optional[str] = None)
Initialize the service with Supabase credentials.
Parameters:
SUPABASE_URLSUPABASE_SERVICE_KEY
QueryEventService instance
async log_retrieval(user_id, query, source, tools, scores, timeout)
Log a tool retrieval to the query_events table.
Parameters:
Returns:
str | None — Inserted event ID (UUID) or None if failed
Side Effects:
- Writes to Supabase
query_eventstable - Logs warnings if HTTP/network fails (non-blocking)
- Returns gracefully if Supabase unavailable
is_ready() -> bool
Check if service is properly configured (has Supabase credentials).
Returns: True if ready, False otherwise
Example:
Query Events Table Schema
Querying Query Events
Via Supabase REST API
Get user’s recent queries:Via Python
Errors
401 Unauthorized
Cause: Missing or invalid API key Response:Authorization: Bearer {api_key} header
400 Bad Request
Cause: Missing required parameters Response:500 Internal Server Error
Cause: Backend error (e.g., Supabase unavailable) Response:Rate Limiting
Query event logging does NOT count against rate limits (fire-and-forget background logging).
Authentication
All endpoints require authentication:Best Practices
1. Always Include Source Header
Help analytics by identifying request origin:2. Handle Gracefully
Query event logging is non-blocking and fire-and-forget:3. Monitor Query Volume
Track queries per minute and patterns:4. Archive Old Events
Keep dashboard responsive by archiving:Related Docs
Key Takeaway: Query events provide complete visibility into tool retrieval patterns. They’re logged automatically and never block requests. Use them for analytics, debugging, and monitoring. 📊