Skip to main content

What is the serve operation

The .serve operation connects your local Cognee Python SDK to Cognee Cloud or another remote Cognee instance. After cognee.serve() connects, SDK calls such as remember(), recall(), improve(), and forget() run against the remote instance instead of local storage. This lets the same Python code work with local Cognee during development and with a hosted or self-hosted Cognee backend in production.
import cognee

await cognee.serve()
The CLI command cognee serve starts a local Cognee backend process. The Python function cognee.serve() connects the SDK client to a remote or local backend.

Where serve fits

  • Use serve() when you want SDK operations to target Cognee Cloud.
  • Use it when you want to connect to a self-hosted Cognee API server.
  • Use it before Push when you want push() to reuse saved or active remote credentials.
  • Use disconnect() when you want the SDK to return to local execution.
  • Use syncing a local instance for a cloud-focused walkthrough of the same connection flow.

What happens under the hood

  1. Resolve connection settings - Cognee looks for an explicit URL/API key, environment variables, saved credentials, or a browser login flow.
  2. Create a remote client - the SDK stores a client that knows how to call the remote Cognee API.
  3. Route SDK operations remotely - supported high-level operations execute against the connected instance.
  4. Persist credentials when applicable - Cloud login credentials can be saved and reused on later runs.
  5. Disconnect on request - cognee.disconnect() clears the active remote client and returns the SDK to local mode.

Connection modes

Call serve() without arguments to use the Cognee Cloud login flow.
import cognee

await cognee.serve()
After login, Cognee stores reusable credentials at ~/.cognee/cloud_credentials.json.

After serve connects

Supported SDK operations run on the connected remote instance.
await cognee.serve(
    url="https://your-instance.cognee.ai",
    api_key="your-api-key",
)

await cognee.remember("Einstein developed general relativity in 1915.")
results = await cognee.recall("What did Einstein develop?")

await cognee.disconnect()
serve() changes where SDK operations execute. It does not copy local datasets to the remote instance by itself. Use Push to upload an already-built local graph, or run remember() while connected to ingest data directly into the remote instance.

Examples and details

Cloud login

import cognee

# Opens the browser login flow and discovers your Cloud tenant.
await cognee.serve()

await cognee.remember("Cloud memory note")
results = await cognee.recall("What notes are stored?")
import cognee

await cognee.serve(
    url="https://memory.example.com",
    api_key="ck_live_...",
)

await cognee.remember("Runbook: deploys happen on Tuesdays.")
await cognee.disconnect()

# This now runs against local Cognee storage again.
await cognee.remember("Local-only note")

See also