# Call your agents from code

Source: https://docs.autessa.com/docs/features/agents

> Talk to the agents you deployed on Autessa from TypeScript, Python or Java with the agents package: start a conversation, send turns, read replies, and upload files, images and audio.

# Call your agents from code

(Preview)

The agents package is in private preview and not yet released. Names may change before release.

The agents package talks to the agents you deployed on Autessa: start a conversation, send turns, read the reply, and attach files, images and audio. The agent's own firewall policy runs on Autessa on every turn.

## When to use it

- Your website, app or backend needs to chat with an Autessa agent.
- You built the agent in the builder or [in code](/docs/cdk/agents.md), and want to call it from your own service.
- You want to send documents, images or recordings to an agent along with a message.

## Install

**TypeScript:**

```bash
npm install @autessa/agents
```

During the preview, build the package from the `autessa-js` repository and install the packed files, as its README describes.

**Python:**

```bash
pip install "autessa-core @ git+https://github.com/Autessa-Inc/autessa-python#subdirectory=packages/autessa-core"
pip install "autessa-agents @ git+https://github.com/Autessa-Inc/autessa-python#subdirectory=packages/autessa-agents"
```

After release: `pip install autessa-agents`.

**Java:**

```xml
<dependency>
  <groupId>com.autessa</groupId>
  <artifactId>autessa-agents</artifactId>
  <version>0.1.0-SNAPSHOT</version>
</dependency>
```

During the preview, build `autessa-java` from source first. For Spring Boot, add `autessa-spring-boot-starter` next to it.

Set a key that can use the agent. A developer key from [Get a free API key](https://builder.autessa.com/get-api-key) acts as you, so it can talk to any agent you can open. To limit a key to one agent, create it in **Settings → API keys**. Agents are a [resource-scoped API](/docs/start/api-keys.md#two-kinds-of-api). See [Keys](/docs/start/keys.md).

```bash
export AUTESSA_API_KEY="<keyId>:<secret>"
```

## Start a conversation

**TypeScript:**

```typescript
import { Agents } from "@autessa/agents";

const agents = new Agents(); // reads AUTESSA_API_KEY
const { conversationId, reply } = await agents.start({ agentId: "V1StGXR8Z5", message: "Where is order A1001?" });
console.log(reply);
console.log((await agents.send(conversationId, "Can I change the address?")).reply);
```

**Python:**

```python
from autessa.agents import Agents

agents = Agents()  # reads AUTESSA_API_KEY
first = agents.start("V1StGXR8Z5", "Where is order A1001?")
print(first.reply)
print(agents.send(first.conversation_id, "Can I change the address?").reply)
```

**Java:**

```java
import com.autessa.agents.Agents;
import com.autessa.agents.AgentReply;

Agents agents = Agents.fromEnv(); // AUTESSA_API_KEY
AgentReply first = agents.start("V1StGXR8Z5", "Where is order A1001?");
System.out.println(first.text());
System.out.println(agents.send(first.conversationId(), "Can I change the address?").text());
```

`start` opens a conversation and sends the first message. It also takes `externalId` (your own ID for the conversation) and `metadata`. Both reach the agent's tools as environment variables: `externalId`, and one per metadata key.

## Keep a conversation going

A `Conversation` remembers its ID, so you just keep calling `send`.

**TypeScript:**

```typescript
const chat = await agents.conversation("V1StGXR8Z5");
console.log(await chat.send("Hi, I need help with a refund."));
console.log(await chat.send("Order A1001."));
await chat.close();
```

**Python:**

```python
with agents.conversation("V1StGXR8Z5") as chat:
    print(chat.send("Hi, I need help with a refund.").reply)
    print(chat.send("Order A1001.").reply)
# leaving the with block closes the conversation
```

`AsyncAgents` and `AsyncConversation` do the same as coroutines.

**Java:**

```java
Conversation chat = agents.conversation("V1StGXR8Z5");
System.out.println(chat.send("Hi, I need help with a refund.").text());
System.out.println(chat.send("Order A1001.").text());
chat.close();
```

To pick up a conversation started elsewhere: `agents.conversation(agentId, conversationId)`.

## Send files, images and audio

Upload first, then attach the result to a message. Uploads go straight to a short-lived link with the content type it expects, and your API key isn't sent with them.

**TypeScript:**

```typescript
const pdf = await agents.uploadFile("V1StGXR8Z5", bytes, { fileExtension: "pdf" });
const r = await agents.send(conversationId, [
  { inputType: "TEXT", content: "What does this invoice say?" },
  { inputType: "FILE", ...pdf },
]);
console.log(r.reply);
```

**Python:**

```python
pdf = agents.upload_file("V1StGXR8Z5", "invoice.pdf")
r = agents.send(conversation_id, "What does this invoice say?", attachments=[pdf])
print(r.reply)
```

Bytes need `file_name=` or `extension=`.

**Java:**

```java
UploadedFile pdf = agents.uploadFile("V1StGXR8Z5", Path.of("invoice.pdf"));
System.out.println(agents.send(conversationId, "What does this invoice say?", List.of(pdf)).text());
```

## Reference

| Call | What it does |
| --- | --- |
| `start` | Opens a conversation and sends the first message. Returns the conversation ID and the reply. |
| `send(conversationId, message)` | One turn. Returns the reply text and every output part. |
| `create(agentId)` | Opens a conversation with no message yet (TypeScript and Python). |
| `get(conversationId)` | The conversation's record. |
| `status(conversationId)` | `IN_PROGRESS`, `CLOSED` or `NOT_FOUND`. |
| `close(conversationId)`, `revive(conversationId)` | Ends a conversation, or reopens it. |
| `uploadFile`, `uploadImage`, `uploadAudio` | Uploads a file to attach (`upload_file` and so on in Python). |
| `conversation(agentId)` | A conversation object you keep calling `send` on. |

- A conversation this client didn't start needs the agent ID once: `send(id, message, {}, agentId)` in TypeScript, `send(..., agent_id=...)` in Python, `conversation(agentId, conversationId)` in Java.
- Errors are typed: a key that can't use the agent is an auth error (401), an account out of credit is a budget error (402). Rate limits (429) and server errors (5xx) are retried.
- The package doesn't call the firewall itself. The agent's policy runs on Autessa.

## API reference

The package calls the [Agents API](/docs/reference/agents.md).

## Related

- [Build an agent in code](/docs/cdk/agents.md)
- [Agents in the builder](/docs/guide/agentic/agents.md)
- [API keys and scopes](/docs/start/api-keys.md)
- [Use Autessa in your existing project](/docs/integrations/existing-project.md)

