MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / MyAgent

Class MyAgent

examples/xpander/coding_agent.py:39–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37
38
39class MyAgent:
40 def __init__(self):
41 logger.info("🚀 Initializing MyAgent...")
42
43 # Load config
44 config_path = Path(__file__).parent / "xpander_config.json"
45 config = json.loads(config_path.read_text())
46
47 # Get API keys
48 xpander_key = config.get("api_key") or os.getenv("XPANDER_API_KEY")
49 agent_id = config.get("agent_id") or os.getenv("XPANDER_AGENT_ID")
50 openai_key = os.getenv("OPENAI_API_KEY")
51
52 if not all([xpander_key, agent_id, openai_key]):
53 raise ValueError("Missing required API keys")
54
55 # Initialize
56 self.openai = AsyncOpenAI(api_key=openai_key)
57 xpander_client = XpanderClient(api_key=xpander_key)
58 self.agent_backend: Agent = xpander_client.agents.get(agent_id=agent_id)
59 self.agent_backend.select_llm_provider(LLMProvider.OPEN_AI)
60
61 logger.info(f"Agent: {self.agent_backend.name}")
62 logger.info(f"Tools: {len(self.agent_backend.tools)} available")
63 logger.info("✅ Ready!")
64
65 async def run(self, user_txt_input: str) -> dict:
66 step = 0
67 start_time = time.perf_counter()
68 tokens = Tokens(worker=LLMTokens(0, 0, 0))
69 try:
70 while not self.agent_backend.is_finished():
71 step += 1
72 logger.info(f"Step {step} - Calling LLM...")
73 response = await self.openai.chat.completions.create(
74 model="gpt-4.1",
75 messages=self.agent_backend.messages,
76 tools=self.agent_backend.get_tools(),
77 tool_choice=self.agent_backend.tool_choice,
78 temperature=0,
79 )
80 if hasattr(response, "usage"):
81 tokens.worker.prompt_tokens += response.usage.prompt_tokens
82 tokens.worker.completion_tokens += response.usage.completion_tokens
83 tokens.worker.total_tokens += response.usage.total_tokens
84
85 self.agent_backend.add_messages(response.model_dump())
86 self.agent_backend.report_execution_metrics(llm_tokens=tokens, ai_model="gpt-4.1")
87 tool_calls = self.agent_backend.extract_tool_calls(response.model_dump())
88
89 if tool_calls:
90 logger.info(f"Executing {len(tool_calls)} tools...")
91 tool_results = await asyncio.to_thread(self.agent_backend.run_tools, tool_calls)
92 for res in tool_results:
93 emoji = "✅" if res.is_success else "❌"
94 logger.info(f"Tool result: {emoji} {res.function_name}")
95
96 duration = time.perf_counter() - start_time

Callers 2

on_execution_requestFunction · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…