---
name: agentbyte-llm-client
description: Guide for initializing Agentbyte agents with OpenAI and Azure OpenAI clients. Use this when asked to set up LLM clients, configure authentication (API keys, certificates, Managed Identity), or implement Dependency Injection for model clients.
---

Use this skill when a user needs to initialize an `OpenAIChatCompletionClient` or `AzureOpenAIChatCompletionClient`, select an authentication path, or configure LLM parameters.

## Core Patterns

### 1. OpenAI Client Initialization
Canonical source: `notebooks/concepts/agent/02b-openai-wrapper.ipynb`

#### Method A: Env-based factory (Recommended for simple setup)
```python
from agentbyte.llm.openai import OpenAIChatCompletionClient

# Automatically reads OPENAI_API_KEY from environment
model_client = OpenAIChatCompletionClient.from_api_key(
    model="gpt-4o",
    config={"max_completion_tokens": 500}
)
```

#### Method B: Direct Dependency Injection (For advanced SDK control)
```python
import os
from openai import AsyncOpenAI
from agentbyte.llm.openai import OpenAIChatCompletionClient

raw_client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"], timeout=30.0)
model_client = OpenAIChatCompletionClient(model="gpt-4o", client=raw_client)
```

### 2. Azure OpenAI Client Initialization
Canonical source: `notebooks/concepts/agent/02a-azure-openai-wrapper.ipynb`

#### Method A: Managed Identity (Recommended for Azure-hosted production)
```python
from agentbyte.llm.azure_openai import AzureOpenAIChatCompletionClient

model_client = AzureOpenAIChatCompletionClient.from_default_credential(
    model="gpt-4o-mini"
)
```

#### Method B: Certificate Auth (Service Principal)
```python
import os
from agentbyte.llm.azure_openai import AzureOpenAIChatCompletionClient

model_client = AzureOpenAIChatCompletionClient.from_certificate(
    model="gpt-4o-mini",
    tenant_id=os.environ["AZURE_TENANT_ID"],
    client_id=os.environ["AZURE_CLIENT_ID"],
    certificate_data=os.environ["AZURE_CERT_DATA"]
)
```

## Guardrails
- **Environment Loading:** Always use `load_dotenv(find_dotenv(), override=True)` before initializing clients in scripts or notebooks.
- **Client Reuse:** Prefer creating one `model_client` and passing it to multiple agents rather than re-initializing for every agent.
- **Factory vs. Constructor:** Use factory methods (`from_api_key`, `from_certificate`) for standard auth. Use the constructor only if you need to inject a custom-configured `AsyncOpenAI` or `AsyncAzureOpenAI` instance.
- **Config Dict:** LLM parameters like `temperature`, `max_completion_tokens`, or `seed` should be passed via the `config` dictionary.
