> For the complete documentation index, see [llms.txt](https://docs.ainu.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ainu.pro/core-concepts/providers.md).

# Providers

Providers in AINU are responsible for connecting agents to AI models. They act as the bridge between the framework and external AI services, such as Anthropic, OpenAI, or other supported providers. The `Provider` class in AINU provides a base implementation that can be extended to integrate specific AI services.

***

### Base Provider Class

The `Provider` class is an abstract base class that defines the core functionality for all providers. It is not meant to be used directly. Instead, each specific provider (e.g., Anthropic, OpenAI) extends this class and implements its methods.

#### Key Responsibilities:

* **Language Models**: Provides access to language models via the `languageModel` method.
* **Image Models**: Provides access to image models via the `imageModel` method.
* **Default Model IDs**: Defines default model IDs for language and image models.

#### Key Methods:

| Method                     | Description                                                                 |
| -------------------------- | --------------------------------------------------------------------------- |
| `languageModel(model?)`    | Returns a `LanguageModelV1` instance for the specified or default model ID. |
| `imageModel(model?)`       | Returns an `ImageModelV1` instance for the specified or default model ID.   |
| `defaultLanguageModelId()` | Returns the default language model ID for the provider.                     |
| `defaultImageModelId()`    | Returns the default image model ID for the provider.                        |

***

### Implementing a Provider

To implement a provider, you extend the `Provider` class and instantiate the appropriate provider from the `@ai-sdk` package. The `Anthropic` provider is a good example of this.

#### Example: Anthropic Provider

The `Anthropic` class extends the `Provider` base class and integrates the Anthropic AI service:

```typescript
import { AnthropicProvider, createAnthropic } from "@ai-sdk/anthropic";
import { Provider } from "./base";

export class Anthropic extends Provider<AnthropicProvider, AnthropicProviderSettings, AnthropicMessagesModelId> {
  constructor(options: AnthropicProviderSettings) {
    super({ ...options });
    this.provider = createAnthropic(this.options); // Instantiates the Anthropic provider
  }

  defaultLanguageModelId(): AnthropicMessagesModelId {
    return "claude-3-5-haiku-latest"; // Specifies the default model ID
  }
}
```

#### Key Points:

1. **Instantiation**: The `Anthropic` provider uses the `createAnthropic` method from `@ai-sdk/anthropic` to instantiate the provider.
2. **Default Model ID**: The `defaultLanguageModelId` method specifies the default model ID for the provider.
3. **Base Class Methods**: The `languageModel` and `imageModel` methods from the base class are inherited and can be used directly.

***

### Using a Provider

To use a provider, you simply create an instance of the provider class and pass it to the agent:

```typescript
import { Agent, Anthropic } from "@ainulabs/ainu";

const provider = new Anthropic({
  apiKey: "your-api-key", // Replace with your actual API key
});

const agent = new Agent({
  provider,
});
```

This connects the agent to the Anthropic AI service, allowing it to generate text or perform other tasks.

***

### Summary

Providers are essential for connecting agents to external AI services. The `Provider` base class defines the core functionality, while specific providers like `Anthropic` extend it to integrate with their respective services. By using providers, you can easily switch between AI services or add support for new ones with minimal effort.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ainu.pro/core-concepts/providers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
