Skip to main content

Overview

The Chat Completions endpoint allows UserTrace to interact with your custom chat agents using the OpenAI Chat Completions format. This is the primary endpoint for text-based agent testing.

Endpoint

POST /v1/chat/completions

Request Headers

Content-Type: application/json
Authorization: Bearer your-api-key

Request Body

messages
array
required
Array of message objects representing the conversation history
user_id
string
Unique identifier for the simulated user. Optional if your agent doesn’t require user management.
session_id
string
Session identifier for conversation tracking. Optional if your agent doesn’t require session management.
metadata
object
Additional context about the simulation scenario

Request Examples

Minimal Request

{
  "messages": [
    {
      "role": "user",
      "content": "Hello, I need help"
    }
  ]
}

Full Request with Context

{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful customer service agent."
    },
    {
      "role": "user", 
      "content": "I need help with my order #12345"
    }
  ],
  "user_id": "sim_user_abc123",
  "session_id": "session_xyz789",
  "metadata": {
    "scenario_id": "financial_stress_delivery",
    "persona": "delivery_worker_bengaluru",
    "time_of_day": "morning_rush",
    "stress_level": "high"
  }
}

Response Format

choices
array
required
Array containing the agent’s response
session_id
string
Session identifier echoed from request
chat_id
string
Unique identifier for this conversation
metadata
object
Agent performance metrics
evaluation_metadata
object
Additional data for evaluation purposes

Response Examples

Basic Response

{
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "I'd be happy to help you with your order. Let me look up order #12345 for you."
      },
      "finish_reason": "stop"
    }
  ],
  "session_id": "session_xyz789",
  "chat_id": "chat_456def",
  "metadata": {
    "agent_confidence": 0.95,
    "response_time_ms": 1200,
    "tools_used": ["order_lookup"]
  },
  "evaluation_metadata": {
    "order_found": true,
    "tone": "empathetic",
    "resolution_step": 1,
    "customer_acknowledged": true
  }
}

Response with Tool Calls

{
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "I found your order! Order #12345 is currently being prepared and will be shipped within 2 hours.",
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_order_status",
              "arguments": "{\"order_id\": \"12345\"}",
              "result": {
                "order_id": "12345",
                "status": "preparing",
                "estimated_ship_time": "2 hours",
                "tracking_number": null
              }
            }
          }
        ]
      },
      "finish_reason": "stop"
    }
  ],
  "session_id": "session_xyz789",
  "chat_id": "chat_456def",
  "evaluation_metadata": {
    "tool_called": "get_order_status",
    "tool_success": true,
    "response_included_status": true,
    "customer_informed": true
  }
}

Error Responses

400 Bad Request

{
  "error": {
    "message": "Invalid request format: missing required field 'messages'",
    "type": "invalid_request_error",
    "code": "bad_request"
  }
}

429 Rate Limit

{
  "error": {
    "message": "Rate limit exceeded. Please wait before making another request.",
    "type": "rate_limit_error",
    "code": "rate_limit_exceeded"
  }
}

500 Internal Error

{
  "error": {
    "message": "Internal server error. Please try again later.",
    "type": "internal_error", 
    "code": "server_error"
  }
}

Implementation Notes

Tool Calling

If your agent uses function calling, you must handle all tool execution internally. The simulated user will send simple text requests, and your agent should:
  1. Execute any necessary function calls
  2. Include complete tool call information in the response
  3. Provide the final answer incorporating tool results

Performance Requirements

  • Response Time: Target < 5 seconds for standard requests
  • Throughput: Support 100 RPS for parallel simulations
  • Availability: 99.9% uptime during testing windows

Best Practices

  • Include relevant evaluation_metadata to help with scoring
  • Use consistent session_id for multi-turn conversations
  • Handle edge cases gracefully with appropriate error responses
  • Log requests and responses for debugging
Testing: Use tools like curl, Postman, or our SDK to test your endpoint before connecting to UserTrace.