Skip to main content

Query Events & Observability

Agent-CoreX provides comprehensive observability through the Query Events system, which logs all tool retrieval queries and makes them visible in the dashboard. This enables monitoring, analytics, and debugging of tool selection patterns.

What Are Query Events?

A Query Event is a record of a tool retrieval request. It captures:
  • The Query — What the user asked for (e.g., “deploy to production”)
  • Retrieved Tools — All candidate tools returned by semantic search
  • Selected Tools — Tools actually used by the agent
  • Scores — Relevance scores and ranking metrics
  • Request Source — Origin of the request (CLI/MCP, API, Dashboard)
  • User ID — Which user made the request (for dashboard filtering)
  • Timestamp — When the query was executed

Why Query Events Matter

1. Observability

See exactly what queries your agents are making and which tools they’re selecting.

2. Debugging

When a tool selection seems wrong, check the query event to understand the ranking and available candidates.

3. Analytics

Track which tools are most frequently selected, trending queries, and usage patterns.

4. Performance Monitoring

Identify slow queries or tools that are underperforming.

5. Audit Trail

Maintain a record of agent activity for compliance and security.

Data Flow


Request Source Tracking

Every query event includes a source field that identifies where the request came from: This allows you to distinguish:
  • CLI queries (from agents using the MCP gateway)
  • API queries (from direct HTTP clients)
  • Dashboard queries (from the dashboard UI itself)

Example: CLI Request

The CLI automatically adds the X-Agent-Source: mcp header:
The backend logs this as a “mcp” source query event.

Query Event Schema

Table Structure

Field Details

selected_tools — Array of tool names that were actually used:
scores — Nested map of tool scores (NOT flat floats):
retrieved_tools — Full tool metadata from backend:

Accessing Query Events

Via Dashboard

View query events in the Agent-CoreX Dashboard under QueriesQuery History:
  • Filter by date range
  • Search by query text
  • View ranking and tool selection
  • Analyze trends over time

Via Supabase REST API

Query events directly from Supabase:

Via SDK


Integration Points

Backend (agent-corex-enterprise)

The backend automatically logs to query_events when:
  • /v2/retrieve_tools endpoint is called
  • User is authenticated (has user_id)
  • Supabase credentials are configured
Location: apps/api/routes/v2_retrieval.py

CLI (agent-corex)

The CLI automatically adds the source header: Location: agent_core/gateway/tool_router.py

Dashboard (agent-corex-next)

The dashboard reads from query_events and displays:
  • Query history
  • Tool selection visualization
  • Ranking metrics
  • Trend analysis
Location: app/(dashboard)/dashboard/queries/page.tsx

Configuration

Environment Variables

Set these on the backend server:

Graceful Degradation

If Supabase is unavailable:
  • ✅ Tool retrieval still works
  • ✅ Response returned to user
  • ⚠️ Warning logged to console
  • ❌ Query event not recorded
This is intentional — observability should never block production requests.

Best Practices

1. Anonymous Queries Are Not Logged

Query events require user_id for dashboard filtering. Anonymous (unauthenticated) queries are:
  • ✅ Still processed and return tools
  • ❌ Not logged to query_events
  • ✓ Logged to general query_logs instead

2. Source Headers for External Clients

If you’re building a custom client, include the source header:

3. Monitor Query Volume

High-volume queries can impact backend performance. Monitor:
  • Queries per minute
  • Tools per query
  • Query types and patterns

4. Archive Old Events

Periodically archive old query events to keep dashboards responsive:

Troubleshooting

”No query events appearing in dashboard”

Checklist:
  • User is authenticated (has API key)
  • X-Agent-Source header is correct (optional, defaults to “api”)
  • SUPABASE_URL and SUPABASE_SERVICE_KEY configured on backend
  • Supabase is reachable (check network)
  • Check backend logs for warnings: log_retrieval failed

”Query events missing some fields”

Query events are logged with available data. If a field is missing:
  • It wasn’t provided in the request
  • It failed to extract from the tool data
  • It’s optional (scores, retrieved_tools)

“Why is my anonymous query not logged?”

Query events require user_id. Anonymous (unauthenticated) queries are logged to query_logs table instead for observability.

Next Steps

Tool Retrieval Guide

Learn how tools are selected and ranked.

API Reference

Full endpoint documentation.

Dashboard Guide

How to use the dashboard to monitor queries.

Microservice Extraction

Future: Extracting query events to separate service.

Key Takeaway: Query events provide complete visibility into what your agents are querying and which tools they’re selecting. Use this data to optimize, debug, and monitor your agent systems. 🔍