Agency forms as an emergent characteristic from the relationship of dynamic systems and their environment. It very much depends on the entity expanding their conscious field to encapsulate themselves with their environment.
Human infants form agency very early in life as a result of realizing their actions affect their environment. It arises from the sudden realization that you are part of a larger ecosystem. Your actions affect your environment and you can affect change in your external world with conscious thought and action.
This gives rise to self-directed goals and a core agentic drive leading to a system and level of consciousness that is more than the sum of its parts.