home / skills / sickn33 / antigravity-awesome-skills / azure-ai-agents-persistent-java

azure-ai-agents-persistent-java skill

/skills/azure-ai-agents-persistent-java

This skill helps you manage persistent Azure AI agents in Java by creating threads, messages, runs, and tools across sessions.

npx playbooks add skill sickn33/antigravity-awesome-skills --skill azure-ai-agents-persistent-java

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.6 KB
---
name: azure-ai-agents-persistent-java
description: |
  Azure AI Agents Persistent SDK for Java. Low-level SDK for creating and managing AI agents with threads, messages, runs, and tools.
  Triggers: "PersistentAgentsClient", "persistent agents java", "agent threads java", "agent runs java", "streaming agents java".
package: com.azure:azure-ai-agents-persistent
---

# Azure AI Agents Persistent SDK for Java

Low-level SDK for creating and managing persistent AI agents with threads, messages, runs, and tools.

## Installation

```xml
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-agents-persistent</artifactId>
    <version>1.0.0-beta.1</version>
</dependency>
```

## Environment Variables

```bash
PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project>
MODEL_DEPLOYMENT_NAME=gpt-4o-mini
```

## Authentication

```java
import com.azure.ai.agents.persistent.PersistentAgentsClient;
import com.azure.ai.agents.persistent.PersistentAgentsClientBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;

String endpoint = System.getenv("PROJECT_ENDPOINT");
PersistentAgentsClient client = new PersistentAgentsClientBuilder()
    .endpoint(endpoint)
    .credential(new DefaultAzureCredentialBuilder().build())
    .buildClient();
```

## Key Concepts

The Azure AI Agents Persistent SDK provides a low-level API for managing persistent agents that can be reused across sessions.

### Client Hierarchy

| Client | Purpose |
|--------|---------|
| `PersistentAgentsClient` | Sync client for agent operations |
| `PersistentAgentsAsyncClient` | Async client for agent operations |

## Core Workflow

### 1. Create Agent

```java
// Create agent with tools
PersistentAgent agent = client.createAgent(
    modelDeploymentName,
    "Math Tutor",
    "You are a personal math tutor."
);
```

### 2. Create Thread

```java
PersistentAgentThread thread = client.createThread();
```

### 3. Add Message

```java
client.createMessage(
    thread.getId(),
    MessageRole.USER,
    "I need help with equations."
);
```

### 4. Run Agent

```java
ThreadRun run = client.createRun(thread.getId(), agent.getId());

// Poll for completion
while (run.getStatus() == RunStatus.QUEUED || run.getStatus() == RunStatus.IN_PROGRESS) {
    Thread.sleep(500);
    run = client.getRun(thread.getId(), run.getId());
}
```

### 5. Get Response

```java
PagedIterable<PersistentThreadMessage> messages = client.listMessages(thread.getId());
for (PersistentThreadMessage message : messages) {
    System.out.println(message.getRole() + ": " + message.getContent());
}
```

### 6. Cleanup

```java
client.deleteThread(thread.getId());
client.deleteAgent(agent.getId());
```

## Best Practices

1. **Use DefaultAzureCredential** for production authentication
2. **Poll with appropriate delays** — 500ms recommended between status checks
3. **Clean up resources** — Delete threads and agents when done
4. **Handle all run statuses** — Check for RequiresAction, Failed, Cancelled
5. **Use async client** for better throughput in high-concurrency scenarios

## Error Handling

```java
import com.azure.core.exception.HttpResponseException;

try {
    PersistentAgent agent = client.createAgent(modelName, name, instructions);
} catch (HttpResponseException e) {
    System.err.println("Error: " + e.getResponse().getStatusCode() + " - " + e.getMessage());
}
```

## Reference Links

| Resource | URL |
|----------|-----|
| Maven Package | https://central.sonatype.com/artifact/com.azure/azure-ai-agents-persistent |
| GitHub Source | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/ai/azure-ai-agents-persistent |

Overview

This skill is a low-level Java SDK for creating and managing persistent Azure AI agents with threads, messages, runs, and tools. It exposes synchronous and asynchronous clients to build reusable, long-lived agent workflows and integrate model runs into Java services. The SDK is focused on control, observability, and resource lifecycle management for production agent deployments.

How this skill works

The SDK provides PersistentAgentsClient and PersistentAgentsAsyncClient to create agents, start threads, post messages, and run agents against deployed models. Typical flow: create an agent, open a thread, add user messages, start a run, poll or stream run status, retrieve messages, and delete resources when finished. Authentication uses DefaultAzureCredential and the client connects to your PROJECT_ENDPOINT and model deployment name.

When to use it

  • You need long-lived, reusable agent instances shared across sessions or users.
  • You require explicit control over threads, messages, and run lifecycle in Java services.
  • You want synchronous or asynchronous integration with Azure-hosted model deployments.
  • You must audit or persist full agent conversation history and run metadata.
  • You need to orchestrate tools or multi-step agent workflows programmatically.

Best practices

  • Authenticate with DefaultAzureCredential for robust production credentials.
  • Poll run status with reasonable delays (500ms recommended) to avoid hot loops.
  • Prefer the async client for high-concurrency workloads to improve throughput.
  • Implement full status handling: check for QUEUED, IN_PROGRESS, REQUIRES_ACTION, FAILED, CANCELLED.
  • Clean up threads and agents when no longer needed to avoid resource leakage.

Example use cases

  • A math tutoring service that creates a persistent tutor agent, keeps student threads, and resumes context across sessions.
  • Customer support bots that persist conversation threads and replay message histories for audits.
  • Batch orchestration that runs agents on multiple threads concurrently using the async client.
  • Tool-enabled agents that call internal services and record runs and tool outputs for debugging.
  • Integration tests that programmatically create agents, run scenarios, assert responses, and tear down resources.

FAQ

How do I authenticate the client?

Use DefaultAzureCredential and set PROJECT_ENDPOINT and MODEL_DEPLOYMENT_NAME environment variables; then build PersistentAgentsClient with the endpoint and credential.

Should I use the async client?

Yes for high concurrency and throughput; use the sync client for simple or blocking workflows.