# Class BaseAgent

java.lang.Object
com.google.adk.agents.BaseAgent

Direct Known Subclasses:
  `LlmAgent, LoopAgent, ParallelAgent, PlannerAgent, RemoteA2AAgent, SequentialAgent`

public abstract class BaseAgent extends Object
Base class for all agents.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `BaseAgent.Builder<B extends BaseAgent.Builder<B>>`
  Base Builder for all agents.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BaseAgent(String name, String description, @Nullable List<? extends BaseAgent> subAgents, @Nullable List<? extends Callbacks.BeforeAgentCallback> beforeAgentCallback, @Nullable List<? extends Callbacks.AfterAgentCallback> afterAgentCallback)`
  Creates a new BaseAgent.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `com.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback>`
  `afterAgentCallback()`
  `com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback>`
  `beforeAgentCallback()`
  `com.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback>`
  `canonicalAfterAgentCallbacks()`
  The resolved afterAgentCallback field as a list.
  `com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback>`
  `canonicalBeforeAgentCallbacks()`
  The resolved beforeAgentCallback field as a list.
  `io.reactivex.rxjava3.core.Completable`
  `close()`
  Closes all sub-agents.
  `final String`
  `description()`
  Gets the one-line description of the agent's capability.
  `Optional<BaseAgent>`
  `findAgent(String name)`
  Finds an agent (this or descendant) by name.
  `Optional<BaseAgent>`
  `findSubAgent(String name)`
  Recursively search sub agent by name.
  `static BaseAgent`
  `fromConfig(BaseAgentConfig config, String configAbsPath)`
  Creates a new agent instance from a configuration object.
  `final String`
  `name()`
  Gets the agent's unique name.
  `BaseAgent`
  `parentAgent()`
  Retrieves the parent agent in the agent tree.
  `protected void`
  `parentAgent(BaseAgent parentAgent)`
  Sets the parent agent.
  `BaseAgent`
  `rootAgent()`
  Returns the root agent for this agent by traversing up the parent chain.
  `io.reactivex.rxjava3.core.Flowable<Event>`
  `runAsync(InvocationContext parentContext)`
  Runs the agent asynchronously.
  `protected abstract io.reactivex.rxjava3.core.Flowable<Event>`
  `runAsyncImpl(InvocationContext invocationContext)`
  Agent-specific asynchronous logic.
  `io.reactivex.rxjava3.core.Flowable<Event>`
  `runLive(InvocationContext parentContext)`
  Runs the agent synchronously.
  `protected abstract io.reactivex.rxjava3.core.Flowable<Event>`
  `runLiveImpl(InvocationContext invocationContext)`
  Agent-specific synchronous logic.
  `List<? extends BaseAgent>`
  `subAgents()`
  `AgentEnums.AgentOrigin`
  `toolOrigin()`
  Returns the origin of the tool when this agent is used as a tool.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### BaseAgent

    public BaseAgent(String name, String description, @Nullable List<? extends BaseAgent> subAgents, @Nullable List<? extends Callbacks.BeforeAgentCallback> beforeAgentCallback, @Nullable List<? extends Callbacks.AfterAgentCallback> afterAgentCallback)
    Creates a new BaseAgent.
    Parameters:
      `name` - Unique agent name. Cannot be "user" (reserved).
      `description` - Agent purpose.
      `subAgents` - Agents managed by this agent.
      `beforeAgentCallback` - Callbacks before agent execution. Invoked in order until one doesn't return null.
      `afterAgentCallback` - Callbacks after agent execution. Invoked in order until one doesn't return null.

- ## Method Details

  - ### close

    public io.reactivex.rxjava3.core.Completable close()
    Closes all sub-agents.
    Returns:
      a `Completable` that completes when all sub-agents are closed.

  - ### name

    public final String name()
    Gets the agent's unique name.
    Returns:
      the unique name of the agent.

  - ### description

    public final String description()
    Gets the one-line description of the agent's capability.
    Returns:
      the description of the agent.

  - ### parentAgent

    public BaseAgent parentAgent()
    Retrieves the parent agent in the agent tree.
    Returns:
      the parent agent, or `null` if this agent does not have a parent.

  - ### parentAgent

    protected void parentAgent(BaseAgent parentAgent)
    Sets the parent agent.
    Parameters:
      `parentAgent` - The parent agent to set.

  - ### rootAgent

    public BaseAgent rootAgent()
    Returns the root agent for this agent by traversing up the parent chain.
    Returns:
      the root agent.

  - ### findAgent

    public Optional<BaseAgent> findAgent(String name)
    Finds an agent (this or descendant) by name.
    Returns:
      an `Optional` containing the agent or descendant with the given name, or `Optional.empty()` if not found.

  - ### findSubAgent

    public Optional<BaseAgent> findSubAgent(String name)
    Recursively search sub agent by name.
    Returns:
      an `Optional` containing the sub agent with the given name, or `Optional.empty()` if not found.

  - ### subAgents

    public List<? extends BaseAgent> subAgents()

  - ### beforeAgentCallback

    public com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback> beforeAgentCallback()

  - ### afterAgentCallback

    public com.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback> afterAgentCallback()

  - ### toolOrigin

    public AgentEnums.AgentOrigin toolOrigin()
    Returns the origin of the tool when this agent is used as a tool.
    Returns:
      the tool origin, defaults to "BASE_AGENT".

  - ### canonicalBeforeAgentCallbacks

    public com.google.common.collect.ImmutableList<? extends Callbacks.BeforeAgentCallback> canonicalBeforeAgentCallbacks()
    The resolved beforeAgentCallback field as a list.

    This method is only for use by Agent Development Kit.

  - ### canonicalAfterAgentCallbacks

    public com.google.common.collect.ImmutableList<? extends Callbacks.AfterAgentCallback> canonicalAfterAgentCallbacks()
    The resolved afterAgentCallback field as a list.

    This method is only for use by Agent Development Kit.

  - ### runAsync

    public io.reactivex.rxjava3.core.Flowable<Event> runAsync(InvocationContext parentContext)
    Runs the agent asynchronously.
    Parameters:
      `parentContext` - Parent context to inherit.
    Returns:
      stream of agent-generated events.

  - ### runLive

    public io.reactivex.rxjava3.core.Flowable<Event> runLive(InvocationContext parentContext)
    Runs the agent synchronously.
    Parameters:
      `parentContext` - Parent context to inherit.
    Returns:
      stream of agent-generated events.

  - ### runAsyncImpl

    protected abstract io.reactivex.rxjava3.core.Flowable<Event> runAsyncImpl(InvocationContext invocationContext)
    Agent-specific asynchronous logic.
    Parameters:
      `invocationContext` - Current invocation context.
    Returns:
      stream of agent-generated events.

  - ### runLiveImpl

    protected abstract io.reactivex.rxjava3.core.Flowable<Event> runLiveImpl(InvocationContext invocationContext)
    Agent-specific synchronous logic.
    Parameters:
      `invocationContext` - Current invocation context.
    Returns:
      stream of agent-generated events.

  - ### fromConfig

    public static BaseAgent fromConfig(BaseAgentConfig config, String configAbsPath)
    Creates a new agent instance from a configuration object.
    Parameters:
      `config` - Agent configuration.
      `configAbsPath` - Absolute path to the configuration file.
    Returns:
      new agent instance.