# Sessions

> Keep a conversation on Antarya's side and send only the new turn.

## What it is for

By default Antarya is stateless: each request carries the whole conversation, as the [conversations guide](https://antarya.ai/docs/guides/conversations.md) shows. A session keeps the turns for you. You create it once, pass its id as `session_id` and send only the new message. Use one when you do not want to hold and resend the history yourself.

## Create a session and send a turn

```bash
curl https://antarya.ai/v1/sessions \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'

# use the id from the create reply as session_id
curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "session_id": "sess_4f2a9c1e7b3d48a6915e0c2d8f7a6b31",
    "messages": [{"role": "user", "content": "Summarise the termination clause."}]
  }'
```

```python
from antarya import Antarya

client = Antarya()

session = client.sessions.create()

reply = client.chat.create(
    session_id=session.id,
    messages=[{"role": "user", "content": "Summarise the termination clause."}],
)
print(reply.content[0].text)
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const session = await client.sessions.create();

const reply = await client.chat.create({
  session_id: session.id,
  messages: [{ role: "user", content: "Summarise the termination clause." }],
});
console.log(reply.content[0].text);
```

Output:

```
{
  "id": "req_04",
  "content": [
    {"type": "text", "text": "Either side can end the agreement with 30 days of written notice."}
  ],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 18, "output_tokens": 16}
}
```

## Continue it

Send the next turn with the same `session_id`. The saved turns go before your `messages`, so you send only what is new.

```python
reply = client.chat.create(
    session_id=session.id,
    messages=[{"role": "user", "content": "Draft a reply that accepts those terms."}],
)
```

```typescript
const next = await client.chat.create({
  session_id: session.id,
  messages: [{ role: "user", content: "Draft a reply that accepts those terms." }],
});
```

When a request succeeds, the new turns and the reply are saved. A failed or refused request saves nothing, so you can send it again.

## List, read and delete

```bash
curl https://antarya.ai/v1/sessions \
  -H "Authorization: Bearer $ANTARYA_API_KEY"
```

```python
page = client.sessions.list(limit=20)
for s in page.data:
    print(s.id)
if page.has_more:
    page = client.sessions.list(limit=20, after=page.data[-1].id)

saved = client.sessions.get(session.id)
print(saved.messages)

client.sessions.delete(session.id)
```

```typescript
let page = await client.sessions.list({ limit: 20 });
for (const s of page.data) console.log(s.id);
if (page.has_more) page = await client.sessions.list({ limit: 20, after: page.data.at(-1)!.id });

const saved = await client.sessions.get(session.id);
console.log(saved.messages);

await client.sessions.delete(session.id);
```

`has_more` is true when another page exists. Deleting a session removes the turns stored in it.

## Limits

- An account holds at most 1,000 sessions, and every key on the account can use them.
- Paging, the session object and the other fields are in the [sessions reference](https://antarya.ai/docs/api/sessions.md).

## Next steps

- [Sessions reference](https://antarya.ai/docs/api/sessions.md): The four endpoints, the object and paging.
- [Conversations](https://antarya.ai/docs/guides/conversations.md): Keep the history yourself instead.
- [Streaming](https://antarya.ai/docs/guides/streaming.md): Show the reply as it is written.
