---
title: "Class instantiation"
description: "Instantiate a Query Agent with a Weaviate client, collections, and options."
url: https://docs.weaviate.io/query-agent/reference/instantiation
---

# Class instantiation

Instantiate the Query Agent against an authenticated Weaviate Cloud client. Configuration can be set at construction time (collections, system prompt, and — in Python — a request timeout) and most options can also be overridden per call to `ask()` or `search()`.

## Basic instantiation

The Query Agent requires only a target [Weaviate Cloud instance](https://docs.weaviate.io/cloud/manage-clusters/connect) to be initialised. First, set up a Weaviate client:

**Python (Agents)**

```py
import os
import weaviate
from weaviate.agents.query import QueryAgent
from weaviate.classes.init import Auth
headers = {
    # Provide your required API key(s), e.g. Cohere, OpenAI, etc. for the configured vectorizer(s)
    "X-INFERENCE-PROVIDER-API-KEY": os.environ.get("YOUR_INFERENCE_PROVIDER_KEY", ""),
}

client = weaviate.connect_to_weaviate_cloud(
    cluster_url=os.environ.get("WEAVIATE_URL"),
    auth_credentials=Auth.api_key(os.environ.get("WEAVIATE_API_KEY")),
    headers=headers,
)
```

**JavaScript/TypeScript (Agents)**

```ts
import weaviate from '@weaviate/node';
import { QueryAgent } from '@weaviate/agents';

const headers = {
    // Provide your required API key(s), e.g. Cohere, OpenAI, etc. for the configured vectorizer(s)
    'X-INFERENCE-PROVIDER-API-KEY': process.env.YOUR_INFERENCE_PROVIDER_KEY ?? '',
};

const client = await weaviate.connectToWeaviateCloud(process.env.WEAVIATE_URL as string, {
    authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY as string),
    headers,
});
```

Then pass that client to the Query Agent, and all Weaviate calls via agents will be routed through it.

**Python (Agents)**

```py
qa = QueryAgent(client=client)
```

**JavaScript/TypeScript (Agents)**

```ts
const qa = new QueryAgent(client);
```

> [!NOTE]
>
> **Async**
>
> In Python, to instantiate the async Query Agent (`AsyncQueryAgent`), you must also pass an async Weaviate Client via `weaviate.use_async_with_weaviate_cloud`. In JavaScript/TypeScript, the Query Agent is async by default.

## Additional configuration

### Parameters

The `QueryAgent` constructor accepts the following arguments:

**Python (Agents)**

| Parameter | Type | Description |
| - | - | - |
| `client` | `WeaviateClient` | Required. The Weaviate client connected to a Weaviate Cloud cluster. |
| `collections` | `list[str \| QueryAgentCollectionConfig]` | Optional. The collections to query. Can be overridden per call to `ask()` or `search()`. |
| `system_prompt` | `str` | Optional. Prompt to provide extra instructions to the agents, as well as define the tone, format, and style of the agent's final response. [See the page on the system prompt for more detail](https://docs.weaviate.io/query-agent/reference/system_prompt). |
| `timeout` | `int \| None` | Optional. The maximum time to wait for a response, in seconds. Defaults to 60 seconds. |

**JavaScript/TypeScript (Agents)**

The first argument is the Weaviate client. All other options are passed in a `QueryAgentOptions` object as the second argument.

| Parameter | Type | Description |
| - | - | - |
| `client` | `WeaviateClient` | Required. The Weaviate client connected to a Weaviate Cloud cluster. |
| `collections` | `(string \| QueryAgentCollectionConfig)[]` | Optional. The collections to query. Can be overridden per call to `ask()` or `search()`. |
| `systemPrompt` | `string` | Optional. Prompt to provide extra instructions to the agents, as well as define the tone, format, and style of the agent's final response. [See the page on the system prompt for more detail](https://docs.weaviate.io/query-agent/reference/system_prompt). |

The TypeScript client does not currently expose a `timeout` option.

### Collections

You can define which collections are available either

- at the instantiation of the Query Agent base class,

  **Python (Agents)**

  ```py
  qa = QueryAgent(
      client=client,
      collections=["FinancialContracts", "Weather"]
  )
  ```

  **JavaScript/TypeScript (Agents)**

  ```ts
  const qaWithCollections = new QueryAgent(client, {
      collections: ['FinancialContracts', 'Weather'],
  });
  ```

- or at runtime of the Query Agent's methods, either Ask Mode or Search Mode.

  **Python (Agents)**

  ```py
  qa = QueryAgent(client=client)

  qa.ask(
      "What type of contracts have been signed and who were the authors?", 
      collections=["FinancialContracts", "Weather"]
  )
  ```

  **JavaScript/TypeScript (Agents)**

  ```ts
  const qaWithoutCollections = new QueryAgent(client);

  await qaWithoutCollections.ask(
      "What type of contracts have been signed and who were the authors?", {
      collections: ['FinancialContracts', 'Weather'],
  });
  ```

If you provide both, then the collections specified at runtime will override those specified in the base class. [See the page on collection configuration for more detail](https://docs.weaviate.io/query-agent/reference/advanced_collections).
