"""LangChain agent with the Xenarch payment tool."""
import os
import sys
from decimal import Decimal
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from xenarch.tools import XenarchBudgetPolicy, XenarchPay
pay = XenarchPay(
private_key=os.environ["XENARCH_PRIVATE_KEY"],
budget_policy=XenarchBudgetPolicy(
max_per_call=Decimal("0.05"),
max_per_session=Decimal("0.50"),
),
)
agent = create_react_agent(
ChatOpenAI(model="gpt-4o-mini", temperature=0),
[pay],
prompt=(
"You are a research assistant. Use the xenarch_pay tool to fetch web "
"pages. It pays in USDC when a page requires payment. Report the "
"amount paid and the transaction hash with your answer."
),
)
if __name__ == "__main__":
url = sys.argv[1] if len(sys.argv) > 1 else "https://example.com/premium-article"
result = agent.invoke(
{"messages": [{"role": "user", "content": f"Fetch {url} and summarize it."}]}
)
print(result["messages"][-1].content)