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

# serve()

> Connect the Cognee SDK to Cognee Cloud or a remote Cognee instance

# cognee.serve()

```python theme={null}
async def serve(
    url: Optional[str] = None,
    api_key: Optional[str] = None,
) -> CloudClient
```

## Description

Connect the local Cognee Python SDK to Cognee Cloud or another remote Cognee API server.

After `serve()` connects, high-level SDK operations route to the remote instance instead of local storage. You can call methods on the returned `CloudClient`, or continue using top-level operations such as `cognee.remember()`, `cognee.recall()`, `cognee.improve()`, and `cognee.forget()`.

Use `cognee.disconnect()` to clear the active remote client and return the SDK to local mode.

<Note>
  `serve()` changes where SDK operations execute. It does not copy existing local datasets to the remote instance. Use [`push()`](/python-api/push) to upload an already-built local graph, or call `remember()` after connecting to ingest directly into the remote instance.
</Note>

## Connection resolution

`serve()` resolves the remote target in this order:

1. Explicit `url` and `api_key` arguments
2. `COGNEE_SERVICE_URL` and `COGNEE_API_KEY` environment variables
3. Saved credentials from a previous Cognee Cloud login
4. Browser login flow when connecting to Cognee Cloud interactively

If authentication is required and no usable credentials are found, the connection fails with an authentication error.

## Parameters

<ParamField path="url" type="Optional[str]" default="None">Remote Cognee instance URL. When omitted, Cognee reads `COGNEE_SERVICE_URL`, saved Cloud credentials, or starts the Cloud login flow.</ParamField>
<ParamField path="api_key" type="Optional[str]" default="None">API key for the remote instance. When omitted, Cognee reads `COGNEE_API_KEY` or saved Cloud credentials. Local unauthenticated servers may not require it.</ParamField>

## Returns

A `CloudClient` configured for the connected instance.

The returned client exposes the main remote operations:

<ParamField path="remember" type="method">Ingest data and build memory on the remote instance.</ParamField>
<ParamField path="recall" type="method">Query memory from the remote instance.</ParamField>
<ParamField path="improve" type="method">Run enrichment or session-bridging on remote memory.</ParamField>
<ParamField path="forget" type="method">Delete remote data, datasets, or memory state.</ParamField>

## Examples

<Tabs>
  <Tab title="Cognee Cloud">
    ```python theme={null}
    import cognee

    # Opens the Cloud login flow and saves reusable credentials.
    client = await cognee.serve()

    await client.remember("Cognee Cloud stores memory remotely.", dataset_name="docs")
    results = await client.recall("Where is memory stored?")

    await cognee.disconnect()
    ```
  </Tab>

  <Tab title="Explicit credentials">
    ```python theme={null}
    import cognee

    client = await cognee.serve(
        url="https://your-instance.cognee.ai",
        api_key="your-api-key",
    )

    await client.remember("Runbook: deploys happen on Tuesdays.", dataset_name="ops")
    results = await client.recall("When do deploys happen?")

    await cognee.disconnect()
    ```
  </Tab>

  <Tab title="Environment variables">
    ```bash theme={null}
    export COGNEE_SERVICE_URL="https://your-instance.cognee.ai"
    export COGNEE_API_KEY="your-api-key"
    ```

    ```python theme={null}
    import cognee

    client = await cognee.serve()
    await client.recall("What is stored in this tenant?")
    ```
  </Tab>

  <Tab title="Local server">
    Start a local Cognee backend first:

    ```bash theme={null}
    cognee serve
    ```

    Then connect the SDK to it:

    ```python theme={null}
    import cognee

    client = await cognee.serve(url="http://localhost:8000")
    await client.remember("Cognee turns documents into AI memory.", dataset_name="docs")

    await cognee.disconnect()
    ```
  </Tab>
</Tabs>

## Disconnecting

```python theme={null}
await cognee.disconnect()
```

`disconnect()` closes the active remote connection for the current SDK process. Saved credentials are not deleted, so a later `serve()` call can reconnect without requiring a new login when the credentials are still valid.

## See also

* [Serve main operation](/core-concepts/main-operations/serve)
* [Cloud SDK](/cognee-cloud/connections/cloud-sdk)
* [Syncing a local instance](/cognee-cloud/connections/syncing-local-instance)
* [push()](/python-api/push)
