Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions examples/langchain_react_agent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"""
Basic LangChain agent using OpenGradient.

Creates a simple ReAct agent with a tool, powered by an OpenGradient LLM.

Usage:
export OG_PRIVATE_KEY="your_private_key"
python examples/langchain_react_agent.py
"""

import os

from langchain_core.tools import tool
Expand All @@ -17,31 +14,25 @@

private_key = os.environ["OG_PRIVATE_KEY"]

# Ensure sufficient OPG allowance for Permit2 spending
llm_client = og.LLM(private_key=private_key)
llm_client.ensure_opg_approval(min_allowance=5)

# Create the OpenGradient LangChain adapter
llm = og.agents.langchain_adapter(
private_key=private_key,
client=llm_client,
model_cid=og.TEE_LLM.GPT_4_1_2025_04_14,
max_tokens=300,
x402_settlement_mode=og.x402SettlementMode.INDIVIDUAL_FULL,
)


# Define a simple tool
@tool
def get_balance(account: str) -> str:
"""Returns the balance for a given account name."""
balances = {"main": "1.25 ETH", "savings": "10.0 ETH", "treasury": "100.0 ETH"}
return balances.get(account, "Account not found")


# Create a ReAct agent with the tool
agent = create_react_agent(llm, [get_balance])

# Run the agent
result = agent.invoke({"messages": [("user", "What is the balance of my 'treasury' account?")]})

print(result["messages"][-1].content)