Creating connections for LangGraph agents
LangGraph agents can require connections to external services such as LLM providers, databases, or APIs. You define these connection requirements in the agent factory function, and watsonx Orchestrate validates required agent-level connections at runtime.
Before you begin
Before you define the connections required, comply with the following:
- Only team credential-configured connections are supported.
- Connection app IDs in your agent factory function must match the app IDs of the connections that you create. The system does not validate whether selected connections match your agent's requirements.
- Create the required connections before you import the agent.
Creating connections
- In watsonx Orchestrate, go to Connections.
- Click Add connection.
- Configure the connection with the credentials that your LangGraph agent requires.
- Record the app ID and credential type for the connection.
For example, if you create a connection with the app ID openai_api and credential type api_key, your agent accesses the credential as openai_api_api_key.
For more information about creating and managing connections, see Managing connections.
Accessing credentials in factory function
All credentials from connections selected during import are automatically injected at runtime through the config parameter from the factory function. Within the factory funtion, credentials are available at:
credentials = config.get("configurable", {}).get("credentials", {})
Using credentials in your factory function
def create_agent(config: RunnableConfig) -> StateGraph:
# Get all credentials
credentials = config.get("configurable", {}).get("credentials", {})
# Access specific credentials
openai_key = credentials.get("openai_api_api_key")
gemini_key = credentials.get("gemini_api_api_key")
# Use credentials as needed
if openai_key:
llm = ChatOpenAI(api_key=openai_key)
elif gemini_key:
llm = ChatGoogleGenerativeAI(api_key=gemini_key)
else:
raise ValueError("No LLM credentials provided")
# Build and return your graph
workflow = StateGraph(AgentState)
# ... rest of your graph setup
return workflow
The credential key follows the following pattern:
{app_id}_{credential_type}
Where:
app_idis the app ID you specify when creating the connection.credential_typeis the type of credential. The types can be:api_keybasicbearerkey_valueoauth_auth_client_credentials_flowoauth_auth_code_flowoauth_auth_implicit_flowoauth_auth_on_behalf_of_flowoauth_auth_password_flow