SDKs

Python SDK

Install the Python SDK, create a client and call every endpoint from Python 3.10 or later

The neurophic package is a typed client for the REST API. It sends requests with httpx, returns pydantic models, ships a sync and an async client, and needs Python 3.10 or later.

Install

pip install neurophic

Create a client

from neurophic import Neurophic

client = Neurophic(
    api_key="nph_...",
    base_url="https://api.neurophic.ai",
    timeout=10,
)
  • api_key: a project key. Falls back to the NEUROPHIC_API_KEY environment variable; if neither is set the constructor raises ValueError.
  • base_url: where requests go. Falls back to NEUROPHIC_BASE_URL, then the production API.
  • timeout: seconds the client waits for each step of a request, an httpx.Timeout, or None for no limit. Defaults to 30 seconds.
  • http_client: your own httpx.Client, for a proxy or event hooks. It keeps its own timeout unless you set timeout.

Use the client as a context manager, or call close(), to release its connections.

Note:Pass identifier as the first argument of every call. Keep keys server-side; see Get started > API keys.

Every date argument takes an ISO 8601 string, a date or a datetime; a naive datetime is sent as UTC. Wherever a role or rerank mode is accepted, pass a plain string or the MemoryRole and RerankMode enums.

Memory

Ingest

Stores a memory. The write is queued, not written on the spot.

receipt = client.ingest(
    "user-123",
    "Prefers window seats on long flights.",
    name="Ada",
    occurred_at="2026-08-12T09:12:00Z",
    role="user",
)

print(receipt.id, receipt.status)  # the memory id once the write is processed, "queued"
  • occurred_at: when the content was said. Defaults to now; set it when backfilling history, oldest first.
  • role: who said it, user by default. Store your agent's own replies as assistant; they never replace what the person said.

Learn more under API reference > Ingest.

Context

Fetches a short, ready-to-prompt summary for a query, as a string. It is empty when nothing is relevant.

context = client.context(
    "user-123",
    "Booking a flight to Lisbon next month",
    sources=True,
    rerank="fast",
)
  • roles: whose words to include, the person's own by default. Add "assistant" for what your agent said.
  • sources: also return the original turns, each once: the turns behind the block, plus up to five that match the query directly.
  • rerank: a second ranking stage before the cut, fast or thorough. Off by default; a paid mode adds the reranking model's cost to the request.

Learn more under API reference > Context.

Retrieve

Searches memory. The return type follows format: Markdown ready to drop into a prompt by default, the same results as typed objects with format="json".

markdown = client.retrieve("user-123", "travel preferences")

result = client.retrieve(
    "user-123",
    "travel preferences",
    goal="book a flight for the user",
    since="2026-08-01T00:00:00Z",
    roles=["user", "assistant"],
    limit=10,
    reference_date="2026-08-19",
    format="json",
    sources=True,
    rerank="fast",
)

for memory in result.memories:
    print(memory.content)
  • since, until: only memories observed in that window.
  • roles: whose words to return, the person's own by default. Add "assistant" for what your agent said; patterns are always the person's.
  • sources: also return the original turns, each once, as result.sources, with source_id on each memory they were taken from.
  • rerank: a second ranking stage before the cut, fast or thorough. Off by default; a paid mode adds the reranking model's cost to the request, and limit still applies after it.
  • reference_date: the day relative time in the query, such as "last week", is read from. Defaults to today.

With format="json" the result is a RetrieveResult holding memories, observations, patterns and sources, as lists of RetrievedMemory, RetrievedObservation, RetrievedPattern and RetrievedSource. Timestamps are datetime, event_date is a date, and each role is a MemoryRole, or plain text for a role this version of the client does not know. relevance is None when rerank was used; the order of the list is then the ranking.

Learn more under API reference > Retrieve.

List

Pages through every memory stored for an identifier, newest first.

page = client.memory.list("user-123", page=2, per_page=10)

print(page.total)
print(page.memories[0].content)

Learn more under API reference > List.

Get

Fetches one memory by id.

memory = client.memory.get("5f1d2c3e-...", identifier="user-123")

Learn more under API reference > Get.

Delete

Deletes one memory by id.

client.memory.delete("5f1d2c3e-...", identifier="user-123")

Pass restore_previous=True to delete only this version and restore the one it superseded.

Learn more under API reference > Delete.

Async

AsyncNeurophic has the same calls and options, awaited. Close it with aclose(), or use it as an async context manager.

from neurophic import AsyncNeurophic

async with AsyncNeurophic() as client:
    await client.ingest("user-123", "Prefers window seats on long flights.")
    context = await client.context("user-123", "Booking a flight to Lisbon next month")

Errors

Every HTTP and transport failure raises neurophic.NeurophicError, carrying:

status_code
int
The HTTP status; 0 for timeouts and connection failures.
code
str
The API error code, for example VALIDATION_ERROR; see API reference > Errors. Compare it to the ErrorCode constants. UNKNOWN when the body is not the standard envelope, for example a plain-text 413 from the edge.
message
str
The API message.
details
list[ValidationDetail]
The field-level problems on a VALIDATION_ERROR, each with field and message.
body
Any
The raw response body, decoded from JSON when possible.
retry_after
float | None
Seconds to wait, from the Retry-After header when the API sent one.

A response outside the 2xx range raises the subclass for its status, so you can catch what you expect by name:

StatusClass
400BadRequestError, with details on a validation error
401AuthenticationError
402InsufficientCreditError, with balance and required
403PermissionDeniedError
404NotFoundError
409MemoryLimitError
429RateLimitError
5xxInternalServerError, and ServiceUnavailableError for 503

Three more subclasses cover failures that never reach the API, or that it did not answer as expected:

  • NeurophicTimeoutError. The client stopped waiting; the API may still have handled the request.
  • NeurophicConnectionError. A network failure, with the cause as __cause__.
  • InvalidResponseError. A success status with a body the client cannot read, not JSON or not the expected shape. It usually means the API has changed and the client needs updating.
Tip:The client never retries; add your own backoff if you need it.
from neurophic import BadRequestError, InsufficientCreditError, Neurophic, NeurophicError, NeurophicTimeoutError

client = Neurophic()

try:
    client.ingest("user-123", "Prefers window seats.")
except NeurophicTimeoutError:
    print("No reply in time; the write may still go through.")
except BadRequestError as error:
    print(error.message, error.details)
except InsufficientCreditError as error:
    print(f"Balance {error.balance}, required {error.required}. Top up the project.")
except NeurophicError as error:
    print(error.status_code, error.code, error.message)

Licence

The SDK is MIT-licensed; use of the API it calls is governed by the Terms of Use.