> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://humanloop.com/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://humanloop.com/docs/_mcp/server.

# Create Experiment

POST https://api.humanloop.com/v4/chat-experiment

Reference: https://humanloop.com/docs/v4/api/chats/create-experiment

## Response

### 200

- `data` (list of ChatDataResponse, required) — Array containing the chat responses.
- `provider_responses` (list of any, required) — The raw responses returned by the model provider.
- `project_id` (string, optional) — Unique identifier of the parent project. Will not be provided if the request was made without providing a project name or id
- `num_samples` (integer, optional, default: 1) — The number of chat responses.
- `logprobs` (integer, optional) — Include the log probabilities of the top n tokens in the provider_response
- `suffix` (string, optional) — The suffix that comes after a completion of inserted text. Useful for completions that act like inserts.
- `user` (string, optional) — End-user ID passed through to provider call.
- `usage` (Usage, optional) — Counts of the number of tokens used and related stats.
- `metadata` (map from string to any, optional) — Any additional metadata to record.
- `provider_request` (map from string to any, optional) — The raw request sent to the model provider.
- `session_id` (string, optional) — ID of the session if it belongs to one.
- `tool_choice` (ChatResponseToolChoice, optional) — Controls how the model uses tools. The following options are supported: 'none' forces the model to not call a tool; the default when no tools are provided as part of the model config. 'auto' the model can decide to call one of the provided tools; the default when tools are provided as part of the model config. Providing \{'type': 'function', 'function': \{name': \<TOOL\_NAME>}} forces the model to use the named function.

## Types

### ChatDataResponse

Overwrite DataResponse for chat.

- `id` (string, required) — Unique ID for the model inputs and output logged to Humanloop. Use this when recording feedback later.
- `index` (integer, required) — The index for the sampled generation for a given input. The num_samples request parameter controls how many samples are generated.
- `output` (string, required) — Output text returned from the provider model with leading and trailing whitespaces stripped.
- `raw_output` (string, required) — Raw output text returned from the provider model.
- `model_config_id` (string, required) — The model configuration used to create the generation.
- `output_message` (ChatMessageWithToolCall, required) — The message returned by the provider.
- `inputs` (map from string to any, optional) — The inputs passed to the chat template.
- `finish_reason` (string, optional) — Why the generation ended. One of 'stop' (indicating a stop token was encountered), or 'length' (indicating the max tokens limit has been reached), or 'tool_call' (indicating that the model has chosen to call a tool - in which case the tool_call parameter of the response will be populated). It will be set as null for the intermediary responses during a stream, and will only be set as non-null for the final streamed token.
- `tool_results` (list of ToolResultResponse, optional) — Results of any tools run during the generation.
- `messages` (list of ChatMessageWithToolCall, optional) — The messages passed to the to provider chat endpoint.
- `tool_calls` (list of ToolCall, optional) — Deprecated: Please use tool_calls field within the output_message.JSON definition of the tools to call and the corresponding argument values. Will be populated when finish_reason='tool_call'.
- `tool_call` (FunctionTool, optional, deprecated) — Deprecated: Please use tool_calls field within the output_message.JSON definition of the tool to call and the corresponding argument values. Will be populated when finish_reason='tool_call'.

### Usage

- `prompt_tokens` (integer, required) — Number of tokens used in the prompt.
- `generation_tokens` (integer, required) — Number of tokens produced by the generation.
- `total_tokens` (integer, required) — Total number of tokens used by the prompt and generation combined.
- `reasoning_tokens` (integer, optional) — Number of tokens used for model used to 'think' before creating a response to the prompt.

### ChatResponseToolChoice

Controls how the model uses tools. The following options are supported: 'none' forces the model to not call a tool; the default when no tools are provided as part of the model config. 'auto' the model can decide to call one of the provided tools; the default when tools are provided as part of the model config. Providing \{'type': 'function', 'function': \{name': \<TOOL\_NAME>}} forces the model to use the named function.

### ChatMessageWithToolCall

- `role` (enum, required) — Role of the message author.
  - Allowed values: `user`, `assistant`, `system`, `tool`, `developer`
- `content` (Content, optional) — The content of the message.
- `name` (string, optional) — Optional name of the message author.
- `tool_call_id` (string, optional) — Tool call that this message is responding to.
- `tool_calls` (list of ToolCall, optional) — A list of tool calls requested by the assistant.
- `thinking` (list of ChatMessageWithToolCallThinkingItem, optional) — Model's chain-of-thought for providing the response. Present on assistant messages if model supports it.
- `tool_call` (FunctionTool, optional, deprecated) — NB: Deprecated in favour of tool_calls. A tool call requested by the assistant.

### ToolResultResponse

A result from a tool used to populate the prompt template

- `id` (string, required)
- `name` (string, required)
- `signature` (string, required)
- `result` (string, required)

### ToolCall

A tool call to be made.

- `id` (string, required)
- `type` ("function", required) — The type of tool to call.
- `function` (FunctionTool, required) — A function tool to be called by the model where user owns runtime.

### FunctionTool

A function tool to be called by the model where user owns runtime.

- `name` (string, required)
- `arguments` (string, optional)

### ToolChoice

Tool choice to force the model to use a tool.

- `type` ("function", required) — The type of tool to call.
- `function` (FunctionToolChoice, required) — A function tool to be called by the model where user owns runtime.

### Content

The content of the message.

### ChatMessageWithToolCallThinkingItem

- `type`: `thinking`
  - `signature` (string, required) — Cryptographic signature that verifies the thinking block was generated by Anthropic.
  - `thinking` (string, required) — Model's chain-of-thought for providing the response.
- `type`: `redacted_thinking`
  - `data` (string, required) — Thinking block Anthropic redacted for safety reasons. User is expected to pass the block back to Anthropic

### FunctionToolChoice

A function tool to be called by the model where user owns runtime.

- `name` (string, required)

## Examples

**Response**

```json
{
  "data": [
    {
      "id": "id",
      "index": 1,
      "output": "output",
      "raw_output": "raw_output",
      "model_config_id": "model_config_id",
      "output_message": {
        "role": "user"
      },
      "inputs": {
        "key": "value"
      },
      "finish_reason": "finish_reason",
      "tool_results": [
        {
          "id": "id",
          "name": "name",
          "signature": "signature",
          "result": "result"
        }
      ],
      "messages": [
        {
          "role": "user"
        }
      ],
      "tool_calls": [
        {
          "id": "id",
          "type": "function",
          "function": {
            "name": "name"
          }
        }
      ],
      "tool_call": {
        "name": "name"
      }
    }
  ],
  "provider_responses": [
    {
      "key": "value"
    }
  ],
  "project_id": "project_id",
  "num_samples": 1,
  "logprobs": 1,
  "suffix": "suffix",
  "user": "user",
  "usage": {
    "prompt_tokens": 1,
    "generation_tokens": 1,
    "total_tokens": 1,
    "reasoning_tokens": 1
  },
  "metadata": {
    "key": "value"
  },
  "provider_request": {
    "key": "value"
  },
  "session_id": "session_id",
  "tool_choice": "none"
}
```

**SDK Code**

```python
import requests

url = "https://api.humanloop.com/v4/chat-experiment"

response = requests.post(url)

print(response.json())
```

```javascript
const url = 'https://api.humanloop.com/v4/chat-experiment';
const options = {method: 'POST'};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"net/http"
	"io"
)

func main() {

	url := "https://api.humanloop.com/v4/chat-experiment"

	req, _ := http.NewRequest("POST", url, nil)

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.humanloop.com/v4/chat-experiment")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.humanloop.com/v4/chat-experiment")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.humanloop.com/v4/chat-experiment');

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.humanloop.com/v4/chat-experiment");
var request = new RestRequest(Method.POST);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let request = NSMutableURLRequest(url: NSURL(string: "https://api.humanloop.com/v4/chat-experiment")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```