in agents/agents-core/src/commonMain/kotlin/ai/koog/agents/ext/agent/AIAgentStrategies.kt [122:176]
public fun reActStrategy(
reasoningInterval: Int = 1,
name: String = "re_act"
): AIAgentGraphStrategy<String, String> = strategy(name) {
require(reasoningInterval > 0) { "Reasoning interval must be greater than 0" }
val reasoningStepKey = createStorageKey<Int>("reasoning_step")
val nodeSetup by node<String, String> {
storage.set(reasoningStepKey, 0)
it
}
val nodeCallLLM by node<Unit, Message.Response> {
llm.writeSession {
requestLLM()
}
}
val nodeExecuteTool by nodeExecuteTool()
val reasoningPrompt = "Please give your thoughts about the task and plan the next steps."
val nodeCallLLMReasonInput by node<String, Unit> { stageInput ->
llm.writeSession {
appendPrompt {
user(stageInput)
user(reasoningPrompt)
}
requestLLMWithoutTools()
}
}
val nodeCallLLMReason by node<ReceivedToolResult, Unit> { result ->
val reasoningStep = storage.getValue(reasoningStepKey)
llm.writeSession {
appendPrompt {
tool {
result(result)
}
}
if (reasoningStep % reasoningInterval == 0) {
appendPrompt {
user(reasoningPrompt)
}
requestLLMWithoutTools()
}
}
storage.set(reasoningStepKey, reasoningStep + 1)
}
edge(nodeStart forwardTo nodeSetup)
edge(nodeSetup forwardTo nodeCallLLMReasonInput)
edge(nodeCallLLMReasonInput forwardTo nodeCallLLM)
edge(nodeCallLLM forwardTo nodeExecuteTool onToolCall { true })
edge(nodeCallLLM forwardTo nodeFinish onAssistantMessage { true })
edge(nodeExecuteTool forwardTo nodeCallLLMReason)
edge(nodeCallLLMReason forwardTo nodeCallLLM)
}