Acontext
FeaturesMessages

Multi-provider Messages

Store and retrieve messages from OpenAI, Anthropic, and Gemini formats

Acontext stores messages in OpenAI, Anthropic, or Gemini format and automatically converts between them on retrieval.

Store Messages

Create a session

import os
from acontext import AcontextClient

client = AcontextClient(
    api_key=os.getenv("ACONTEXT_API_KEY"),
)
session = client.sessions.create()
import { AcontextClient } from '@acontext/acontext';

const client = new AcontextClient({
    apiKey: process.env.ACONTEXT_API_KEY,
});
const session = await client.sessions.create();

You can create a session with a specific UUID:

session = client.sessions.create(use_uuid="123e4567-e89b-12d3-a456-426614174000")
const session = await client.sessions.create({ useUuid: "123e4567-e89b-12d3-a456-426614174000" });

This is useful for correlating sessions with external systems. A 409 Conflict error is returned if the UUID already exists.

Store messages in any format

client.sessions.store_message(
    session_id=session.id,
    blob={"role": "user", "content": "What is the capital of France?"},
    format="openai"
)
await client.sessions.storeMessage(session.id,
    { role: "user", content: "What is the capital of France?" },
    { format: "openai" }
);
client.sessions.store_message(
    session_id=session.id,
    blob={
        "role": "user",
        "content": [{"type": "text", "text": "Explain quantum computing"}]
    },
    format="anthropic"
)
await client.sessions.storeMessage(session.id, {
    role: "user",
    content: [{ type: "text", text: "Explain quantum computing" }]
}, { format: "anthropic" });
client.sessions.store_message(
    session_id=session.id,
    blob={
        "role": "user",
        "parts": [{"text": "Explain quantum computing"}]
    },
    format="gemini"
)
await client.sessions.storeMessage(session.id, {
    role: "user",
    parts: [{ text: "Explain quantum computing" }]
}, { format: "gemini" });

You can attach custom metadata to any message using the meta parameter:

client.sessions.store_message(
    session_id=session.id,
    blob={"role": "user", "content": "Hello"},
    format="openai",
    meta={"source": "web", "request_id": "abc123"}
)
await client.sessions.storeMessage(session.id, {
    blob: { role: "user", content: "Hello" },
    format: "openai",
    meta: { source: "web", requestId: "abc123" }
});

This is useful for tracking message origin, request IDs, or any application-specific data. See Message Meta for more details.

Retrieve Messages

Retrieve in any format—Acontext converts automatically:

# Stored as OpenAI, retrieve as Anthropic
result = client.sessions.get_messages(session_id=session.id, format="anthropic")

for msg in result.items:
    print(f"{msg.role}: {msg.content}")
// Stored as OpenAI, retrieve as Anthropic
const result = await client.sessions.getMessages(session.id, { format: "anthropic" });

for (const msg of result.items) {
    console.log(`${msg.role}: ${msg.content}`);
}

Delete Session

client.sessions.delete(session_id)
await client.sessions.delete(sessionId);

Deleting a session permanently removes all messages.

Next Steps

Last updated on

On this page