Connecting LLM Providers
Relevant source files
The following files were used as context for generating this wiki page:The Dyad application integrates with various third-party Large Language Model (LLM) providers to power its AI capabilities. This system is responsible for managing user-provided API keys, selecting the appropriate LLM based on user settings, and instantiating the correct client for communication. The entire process is designed to be modular, allowing for different providers to be used interchangeably.
Configuration is handled through a dedicated settings UI, with the core logic residing in the main process, accessed via Inter-Process Communication (IPC) channels. This separation ensures that sensitive API keys are managed securely in the backend while providing a responsive user experience in the frontend.
Architecture Overview
The system for connecting LLM providers spans both the renderer process (UI) and the main process (backend). The user configures their preferred LLM provider and enters API keys in the Settings page. These settings are sent via IPC to the main process, which stores them securely. When an LLM interaction is required, the main process uses these stored settings to instantiate the correct LLM client from the LangChain library.
This diagram illustrates the high-level data flow from user configuration to LLM client instantiation.
UI Configuration
Users manage LLM provider settings through the Settings page. This page contains components for selecting the provider, model, and chat mode, as well as configuring API keys.
Sources: src/pages/settings.tsx, src/components/settings/ApiKeyConfiguration.tsx
Components
| Component | Description | File |
|---|---|---|
SettingsPage | The main container for all LLM-related settings, including Model Selection, Chat Mode, and API Key Configuration. | src/pages/settings.tsx |
ApiKeyConfiguration | A dedicated component for managing API keys for different providers (OpenAI, Anthropic, Groq). It fetches and saves configurations via IPC. | src/components/settings/ApiKeyConfiguration.tsx |
User Interaction Flow
The following sequence diagram shows how a user's action in the UI leads to the secure storage of their API key in the backend.
The ApiKeyConfiguration component supports the following providers and their specific fields:
- OpenAI: API Key, Base URL (optional)
- Anthropic: API Key
- Groq: API Key
Sources: src/components/settings/ApiKeyConfiguration.tsx:75-125
Backend IPC Handling
The main process exposes several IPC handlers to manage LLM settings and create model clients. These handlers are the bridge between the UI and the core application logic.
Sources: src/ipc/handlers/language_model_handlers.ts
| IPC Channel | Handler Function | Description |
|---|---|---|
getLanguageModelConfig | handleGetLanguageModelConfig | Retrieves the saved LLM provider configurations (API keys, etc.) from the electron-store. |
setLanguageModelConfig | handleSetLanguageModelConfig | Saves the LLM provider configurations to the electron-store. |
getLlmSettings | handleGetLlmSettings | Retrieves chat mode and model selection settings. |
setLlmSettings | handleSetLlmSettings | Saves chat mode and model selection settings. |
getModelClient | handleGetModelClient | The primary handler for creating an LLM client instance based on the current configuration. |
Model Client Instantiation
The getModelClient utility is the core of the provider connection logic. It takes the language model configuration and returns an initialized LangChain client object.
Sources: src/ipc/utils/get_model_client.ts
Logic Flow
The function uses a switch statement to determine which provider's client to instantiate.
Supported Providers and Classes
The system uses the following classes from the LangChain library to connect to LLM providers:
| Provider | LangChain Class | Library |
|---|---|---|
openai | ChatOpenAI | @langchain/openai |
anthropic | ChatAnthropic | @langchain/anthropic |
groq | ChatGroq | @langchain/openai |
Sources: src/ipc/utils/get_model_client.ts:2-4
Chat Modes and System Prompts
The settings configured in the UI also determine the behavior of the AI by selecting different system prompts. The chatMode setting (build, ask, or agent) is used to construct the final prompt sent to the LLM.
Sources: src/prompts/system_prompt.ts, src/pages/settings.tsx:59-86
The getSystemPromptForChatMode function selects the appropriate base prompt:
build: UsesBUILD_SYSTEM_PROMPT, instructing the AI to act as a code editor.ask: UsesASK_MODE_SYSTEM_PROMPT, instructing the AI to act as a helpful assistant that does not write code.agent: UsesAGENT_MODE_SYSTEM_PROMPT, instructing the AI to act as an information-gathering agent.
This mechanism allows the application to tailor the AI's persona and capabilities based on the user's selected task, even when using the same underlying LLM provider. Additionally, prompts can be further modified based on other application states, such as the availability of integrations like Supabase.
Sources: src/prompts/system_prompt.ts:511-526, src/prompts/supabase_prompt.ts