MCPcopy Create free account
hub / github.com/ScaleML/AgentSPEX / LLMExecutor

Class LLMExecutor

src/harness/execution/llm_executor.py:112–1449  ·  view source on GitHub ↗

Executes LLM interactions with multi-step tool calling support. This class is the core execution engine for LLM-driven workflow steps. It manages the conversation with the LLM, handles tool calls (both standard and inline), executes submodules, and supports durable execution via tra

Source from the content-addressed store, hash-verified

110
111
112class LLMExecutor(TracingMixin, ShellUtilsMixin):
113 """Executes LLM interactions with multi-step tool calling support.
114
115 This class is the core execution engine for LLM-driven workflow steps.
116 It manages the conversation with the LLM, handles tool calls (both
117 standard and inline), executes submodules, and supports durable
118 execution via tracing.
119
120 Attributes:
121 mcp_client: MCP client for tool invocation.
122 llm_client: LLMClient for LLM completions (supports multiple providers via LiteLLM).
123 tools: List of available tool definitions.
124 usage_tracker: Dict for tracking token usage across steps.
125 metrics_lock: Threading lock for thread-safe metrics updates.
126
127 Inherited from TracingMixin:
128 _trace_lock: Lock for thread-safe trace writing.
129 _trace_writer: Optional writer for durable execution traces.
130 _llm_replay_events: Events for replay mode.
131 _replay_index: Current position in replay events.
132
133 Example:
134 executor = LLMExecutor(mcp_client, tools, llm_client, usage_tracker)
135 output, tokens = executor.execute_llm_step(
136 instruction="Analyze the code",
137 prev_output="",
138 step_id=1,
139 args=effective_args,
140 logger=logger,
141 context=workflow_context,
142 )
143 """
144
145 def __init__(
146 self,
147 mcp_client: MCPClient,
148 tools: List[Dict[str, Any]],
149 llm_client: LLMClient,
150 usage_tracker: Dict[str, int],
151 ) -> None:
152 """Initialize the LLM executor.
153
154 Args:
155 mcp_client: MCP client for invoking tools.
156 tools: List of tool definitions in OpenAI format.
157 llm_client: LLMClient for chat completions (supports multiple providers).
158 usage_tracker: Mutable dict for accumulating token usage.
159 """
160 self.mcp_client = mcp_client
161 self.llm_client = llm_client
162 self.tools = tools
163 self.usage_tracker = usage_tracker
164 self.metrics_lock = threading.Lock()
165
166 # Reference to interpreter for submodule execution (set by Interpreter after init)
167 self.interpreter: Optional[Any] = None
168
169 # Initialize tracing state (required by TracingMixin)

Callers 2

_make_executorMethod · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 1

_make_executorMethod · 0.72