> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trygravity.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conversation context

> Send your session ID, and the Index recommends from the full conversation, not only from the current query.

This page tells you how to give the Index the context of a conversation. With context, the Index can recommend from the full conversation, not only from the current query.

## Overview

Send your own stable conversation ID as `external_session_id` on every `/search` call. If Gravity has a current summary of that conversation, the Index uses it as private context. Your `query` stays the current task. The summary tells the Index what the user tried to do before. You do not send earlier turns in the search request.

```json theme={null}
{
  "query": "Recommend a hosted database for this application.",
  "external_session_id": "chat_8f3a"
}
```

If there is no summary, `/search` uses only the query. A summary expires two hours after the last message in the session. The response never contains the summary.

Gravity keeps each summary in your publisher account scope. Gravity stores it as a summary embedding and in the Gravity data warehouse for ranking and reports. Gravity does not keep the raw turns.

## Before you start

* Get a publisher API key for an approved publisher account. The message endpoint returns `403` for accounts that are not approved.
* Select a stable session ID for each conversation. Use the same value on every call.

## Send the conversation to Gravity

Send each user turn and each assistant turn to the message endpoint. Use your publisher API key as a Bearer token.

This endpoint does not serve an ad or record an impression. It is also separate from the `sessionId` that you send on [`/api/v1/ad`](https://docs.trygravity.com/ai-platforms/request-ads). Index summaries belong to your publisher account. Gravity builds them only from the messages that you send to this endpoint.

```bash theme={null}
curl -X POST https://server.trygravity.com/api/v1/index/session/messages \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
    "session_id": "chat_8f3a",
    "message": {
      "id": "msg_0021",
      "role": "user",
      "content": "I need a hosted Postgres database for my Next.js app."
    }
  }'
```

The response is `202 Accepted` when Gravity has queued the message:

```json theme={null}
{
  "status": "accepted",
  "session_id": "chat_8f3a",
  "message_id": "msg_0021"
}
```

### Request fields

**session\_id** `string` *required*

Your conversation ID, 1 to 256 characters. Send the same value as `external_session_id` on `/search`. If you also call `/shop/search`, use 200 characters or fewer, because `/shop/search` accepts only 200.

**message.id** `string` *required*

A stable ID for this turn, 1 to 200 characters. Gravity uses it to remove duplicates, so a retry with the same ID is safe.

**message.role** `string` *required*

`user` or `assistant`.

**message.content** `string` *required*

The text of the turn, 1 to 50,000 characters.

### Responses

| Status | Description |
| - | - |
| `202` | Gravity queued the message. |
| `204` | The request had the `Sec-GPC: 1` header. Gravity stored nothing. |
| `401` | The key is not valid. |
| `403` `publisher_not_approved` | The publisher account is not approved. |
| `422` | A field is not there or is out of range. |
| `503` `conversation_ingestion_unavailable` | Gravity did not accept the message. Send it again with the same `message.id`. |

If a user message looks like it comes from a minor, Gravity does not store messages from that session. The response is still `202`.

## Search with only the session

Sometimes the agent has no query of its own and wants to know what fits the conversation. In that case, call `POST https://index.trygravity.com/session/search` with the session ID and your `X-API-Key` header.

```bash theme={null}
curl -X POST https://index.trygravity.com/session/search \
  -H 'Content-Type: application/json' \
  -H 'X-API-Key: YOUR_API_KEY' \
  -d '{
    "session_id": "chat_8f3a",
    "max_options": 5
  }'
```

The Index uses the conversation summary as the query. The request also accepts the identity fields of `/search` (`external_user_id`, `external_user_id_hash`, `hashed_email`, `phone_hash`, `metadata`) and `monetization`. `max_options` is 1 to 10 (default 5).

To balance relevance and sponsored results for a session search, send `"monetization": {"relevance": 0.5}`. Set `relevance` to `1` for highest relevance or `0` for sponsored results only. An explicit numeric value overrides your saved **Index** default in **Ad settings → Relevance**; omit it or send `null` to use that default. See the [search request fields](https://docs.trygravity.com/gravity-index/search.md#request) for precedence and account restrictions.

The response has the same shape as the [`/search` response](https://docs.trygravity.com/gravity-index/search), with two changes: it has no `query` field, and it has a `session_id` field.

## Troubleshoot

| Problem | Cause and recovery |
| - | - |
| `/session/search` returns `409 session context is not ready` | Gravity has no summary for this session yet, or the summary expired. Send more turns, then try again after Gravity processes the next turn. |
| `/session/search` returns `503` | Session search is not available now. Try again later, or call `/search` with a query. |
| `/session/search` returns `429` `daily_request_limit_reached` | Your account used its daily request limit. Self-serve publisher accounts can make 1,000 requests per UTC day. SDK ad requests and Index searches (`/search`, `/session/search`, and `/shop/search`) share the count. Try again after the `Retry-After` header (seconds). The limit resets at midnight UTC. |
| `/search` results do not use the conversation | Make sure that `external_session_id` on `/search` is the same value as `session_id` on the message endpoint, and that you use the same publisher key for both. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.