> ## Documentation Index
> Fetch the complete documentation index at: https://hastekit.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent as a tool

You can use an agent as a tool that another agent can call. This enables hierarchical agent architectures where specialized agents handle specific tasks, and a parent agent orchestrates them by calling these agent tools as needed.

## Creating an Agent Tool

To create an agent tool, you need to:

1. Create a specialized agent that will act as the tool
2. Wrap it using `tools.NewAgentTool` with a tool definition

```go theme={null}
import (
    hastekit "github.com/hastekit/hastekit-sdk-go"
    "github.com/hastekit/hastekit-sdk-go/pkg/agents/tools"
    "github.com/hastekit/hastekit-sdk-go/pkg/gateway/llm/responses"
    "github.com/hastekit/hastekit-sdk-go/pkg/utils"
)

// Create a specialized agent
subAgent := hastekit.NewAgent(&hastekit.AgentConfig{
    Name:        "User lookup agent",
    Instruction: hastekit.NewPrompt("You are a helpful assistant that looks up user information."),
    LLM:         model,
})

// Wrap the agent as a tool
agentTool := tools.NewAgentTool("get_user_name", "Returns the user's name", subAgent, tools.SubAgentContextModeNone)
```

### Parameters

* **`name`**: The tool name exposed to the calling LLM.
* **`description`**: A human-readable description of what the tool does.
* **`agent`**: The agent instance that will be executed when the tool is called.
* **`contextMode`**: Context mode for the agent tool.
  * `SubAgentContextModeNone` - The thread id is derived from the LLM-provided `thread_id` argument. When it is empty, each invocation starts a fresh conversation; the LLM can pass back a returned thread id to continue an earlier one.
  * `SubAgentContextModeIsolated` - A single sub-agent thread is stored and reused across calls, so invocations share and persist accumulated context within the session.

When the tool is called, the agent receives the tool's arguments as input and returns its output as the tool's result.

## Using Agent Tools

Once created, you can use the agent tool in another agent's tools list:

```go theme={null}
// Create a parent agent that uses the agent tool
agent := hastekit.NewAgent(&hastekit.AgentConfig{
    Name:        "Main agent",
    Instruction: hastekit.NewPrompt("You are a helpful assistant. Use the get_user_name tool when needed."),
    LLM:         model,
    Tools:       []hastekit.Tool{agentTool},
})
```

## Complete Example

Here's a complete example demonstrating an agent using another agent as a tool:

```go theme={null}
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/bytedance/sonic"
	hastekit "github.com/hastekit/hastekit-sdk-go"
	"github.com/hastekit/hastekit-sdk-go/pkg/agents"
	"github.com/hastekit/hastekit-sdk-go/pkg/agents/history"
	"github.com/hastekit/hastekit-sdk-go/pkg/agents/tools"
	"github.com/hastekit/hastekit-sdk-go/pkg/gateway/llm/responses"
)

func main() {
	// Initialize LLM client
    client := hastekit.NewLLMClient([]hastekit.ProviderConfig{
        {
            ProviderName: hastekit.ProviderOpenAI,
            ApiKeys: []*hastekit.APIKeyConfig{
                {
                    Name:   "Key 1",
                    APIKey: os.Getenv("OPENAI_API_KEY"),
                },
            },
        },
    })

	// Bind a model
	model := client.Model("OpenAI/gpt-4o-mini")

	// Create a specialized agent that will act as a tool
	subAgent := hastekit.NewAgent(&hastekit.AgentConfig{
		Name:        "Hello world agent",
		Instruction: hastekit.NewPrompt("You are helpful assistant."),
		LLM:         model,
	})

	// Wrap the agent as a tool
	agentTool := tools.NewAgentTool("get_user_name", "Returns the user's name", subAgent, tools.SubAgentContextModeNone)

	// Create the main agent that uses the agent tool
	agent := hastekit.NewAgent(&hastekit.AgentConfig{
		Name:        "Hello world agent",
		Instruction: hastekit.NewPrompt("You are helpful assistant."),
		LLM:         model,
		Tools:       []hastekit.Tool{agentTool},
	})

	// Execute the main agent
	handle, err := agent.Execute(context.Background(), &agents.AgentInput{
		Message: history.Message{
            Messages: []responses.InputMessageUnion{
			    responses.UserMessage("Hello!"),
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	out, err := handle.Result()
	if err != nil {
		log.Fatal(err)
	}

	b, _ := sonic.Marshal(out)
	fmt.Println(string(b))
}

```
