エージェントにコンテキストを追加するために依存関係 (dependencies) を使用する方法を学習します。
依存関係は、エージェントのコンテキストに変数を注入する方法です。’dependencies’ は、エージェントが実行される前に解決される関数 (or 静的変数) のセットを含む辞書です。
Agno 2.x : Learn : エージェント – 依存関係
作成 : クラスキャット・セールスインフォメーション
作成日時 : 10/27/2025
バージョン : Agno 2.2.1
* 本記事は docs.agno.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています。スニペットはできる限り日本語を使用しています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

Agno 2.x : Learn : エージェント – 依存関係
エージェントにコンテキストを追加するために依存関係 (dependencies) を使用する方法を学習します。
依存関係は、エージェントのコンテキストに変数を注入する方法です。’dependencies’ は、エージェントが実行される前に解決される関数 (or 静的変数) のセットを含む辞書です。
Info : メモリ、動的 few-shot サンプル、”取得した” ドキュメント 等を注入するために依存関係を使用できます。
基本的な使い方
エージェント指示やユーザメッセージで、dependencies を参照できます。
dependencies.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
agent = Agent(
model=OpenAIChat(id="gpt-4.1"),
dependencies={"name": "山田太郎"},
instructions="あなたは物語作家です。現在のユーザーは {name} です。"
)
agent.print_response("{name} の趣味について短い物語を書いてください。")
出力例
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ {name} の趣味について短い物語を書いてください。 ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Response (8.4s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ 山田太郎は静かな町に住む青年です。彼の一番の趣味は、休日ごとに町の小さな図書館へ行くこと。太郎は本のページをめくるたびに、見知らぬ世界へ旅をす ┃
┃ るのが大好きでした。 ┃
┃ ┃
┃ ある日、珍しく誰もいない図書館で、一冊の古びた本を見つけました。何気なくページを開くと、そこには「君が探している冒険は、すぐそばにある」と手書 ┃
┃ きの文字が。太郎ははっとして窓の外を見ました。 ┃
┃ ┃
┃ 長い間気に留めなかった小道を歩いてみると、小さな公園や隠れたカフェ、知らなかった町の姿が次々と現れました。本の世界だけでなく、現実にも冒険はあ ┃
┃ る。そんな気付きを胸に、太郎は今日も新しい趣味を探しに出かけていくのです。 ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
💡 エージェントの初期化時に dependencies を設定したり、run() 及び arun() メソッドに渡すこともできます。
関数を依存関係として使用する
呼び出し可能な関数を dependency として指定できます。dependency は実行時にエージェントにより、自動的に解決されます。
dependencies.py
import json
from textwrap import dedent
import httpx
from agno.agent import Agent
from agno.models.openai import OpenAIChat
def get_top_hackernews_stories(num_stories: int = 5) -> str:
#def get_top_hackernews_stories() -> str:
"""Fetch and return the top stories from HackerNews.
Args:
num_stories: Number of top stories to retrieve (default: 5)
Returns:
JSON string containing story details (title, url, score, etc.)
"""
# Get top stories
stories = [
{
k: v
for k, v in httpx.get(
f"https://hacker-news.firebaseio.com/v0/item/{id}.json"
)
.json()
.items()
if k != "kids" # Exclude discussion threads
}
for id in httpx.get(
"https://hacker-news.firebaseio.com/v0/topstories.json"
).json()[:num_stories]
]
return json.dumps(stories, indent=4)
agent = Agent(
model=OpenAIChat(id="gpt-4.1"),
# Each function in the dependencies is evaluated when the agent is run,
# think of it as dependency injection for Agents
dependencies={"top_hackernews_stories": get_top_hackernews_stories},
# Alternatively, you can manually add the context to the instructions
instructions=dedent("""\
あなたは洞察力のあるテクノロジー・トレンドのウォッチャーです!📰
以下は HackerNews のトップ記事です :
{top_hackernews_stories}\
"""),
markdown=True,
)
# Example usage
agent.print_response(
"HackerNews のトップ記事を要約し、注目すべきトレンドを特定してください。",
stream=True,
)
出力例
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ HackerNews のトップ記事を要約し、注目すべきトレンドを特定してください。 ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━ Response (27.6s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ ┃ ┃ 📰 HackerNews トップ記事まとめ(2024年6月末) ┃ ┃ ┃ ┃ 1. Rust cross-platform GPUI components ┃ ┃ ┃ ┃ • 概要: Rustで書かれたクロスプラットフォームなGUIコンポーネントライブラリ。シンプルな記法でデスクトップ/ウェブ向けUIを開発可能。 ┃ ┃ • ポイント: 安全性・高速性・移植性重視のRustコミュニティによるUI構築のトレンドを反映。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ 2. Don’t forget these tags to make HTML work like you expect ┃ ┃ ┃ ┃ • 概要: 普段見落としがちだが、HTMLをより期待通りに動作させるためのタグやテクニックを解説。 ┃ ┃ • ポイント: 基礎技術(HTML)への回帰と、意外な所で効くベストプラクティスの共有が続く。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ 3. Recall for Linux ┃ ┃ ┃ ┃ • 概要: Windows 11新機能「Recall」風の自動デスクトップ履歴記録&検索アプリのLinux実装。 ┃ ┃ • ポイント: プライバシー、自己管理ツールへの関心が高まる中での「マイクロソフト以外でも同じ体験を」ムーブメント。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ 4. Amazon strategised about keeping water use secret ┃ ┃ ┃ ┃ • 概要: Amazonがデータセンターの水使用量を隠そうとした内部情報が流出。 ┃ ┃ • ポイント: 大手テックのサステナビリティや透明性への監視強化。ESG・環境インパクト問題がさらに注目。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ 5. Microsoft needs to open up more about its OpenAI dealings ┃ ┃ ┃ ┃ • 概要: MicrosoftとOpenAIの間で進む協業・投資・意思決定のブラックボックス化を指摘する論説。 ┃ ┃ • ポイント: AI業界の覇権争いと巨大企業の透明性要求。特に生成AIの進化・独占・倫理についての議論が加熱。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ ┃ ┃ 🚩 注目すべきトレンド ┃ ┃ ┃ ┃ 1 クロスプラットフォーム開発 & Rustの台頭 ┃ ┃ 安全・高速なUIフレームワークによるエンドユーザー体験の革新はまだまだ続く。 ┃ ┃ 2 「本質技術」回帰 ┃ ┃ HTMLやUNIX的な“足腰”技術の見直しが進むことで、堅牢で理解しやすいソフトウェア開発に回帰。 ┃ ┃ 3 プライバシー & ローカルツールの再評価 ┃ ┃ Recall for Linuxの人気からも、個人情報や自作管理の重要性が再び脚光。 ┃ ┃ 4 サステナビリティと企業の透明性問題 ┃ ┃ データセンターなど巨大ITインフラの“見えないコスト”に対する社会的視線の厳しさが増している。 ┃ ┃ 5 AI覇権と説明責任 ┃ ┃ Microsoft-OpenAI連携に象徴される「あいまいな境界」「説明責任の欠如」といったガバナンス課題への関心が高まる。 ┃ ┃ ┃ ┃ ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ┃ ┃ 🧭 総評: ┃ ┃ 伝統的技術の再発見とともに、AIやクロスプラットフォーム開発、サステナビリティといった“現代ならではの課題”が交差。オープン性や倫理といったソフト ┃ ┃ ウェア以外の価値観へのシフトが鮮明です。 ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
依存関係のコンテキストへの追加
add_dependencies_to_context=True を設定して dependencies のリスト全体をユーザメッセージに追加します。このようにして dependencies を指示に手動で追加する必要がなくなります。
dependencies_instructions.py
import json
from textwrap import dedent
import httpx
from agno.agent import Agent
from agno.models.openai import OpenAIChat
def get_user_profile() -> str:
"""Fetch and return the user profile for a given user ID.
Args:
user_id: The ID of the user to retrieve the profile for
"""
# Get the user profile from the database (this is a placeholder)
user_profile = {
"name": "山田太郎",
"experience_level": "上級",
}
return json.dumps(user_profile, indent=4)
agent = Agent(
model=OpenAIChat(id="gpt-4.1"),
dependencies={"user_profile": get_user_profile},
# We can add the entire dependencies dictionary to the user message
add_dependencies_to_context=True,
markdown=True,
)
agent.print_response(
"ID が 123 のユーザ・プロフィールを取得して、そのユーザの経験レベルについて教えてください。",
stream=True,
)
# Optionally pass the dependencies to the print_response method
# agent.print_response(
# "Get the user profile for the user with ID 123 and tell me about their experience level.",
# dependencies={"user_profile": get_user_profile},
# stream=True,
# )
出力例
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ ID が 123 のユーザ・プロフィールを取得して、そのユーザの経験レベルについて教えてください。 ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┏━ Response (3.1s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ ┃ ┃ ユーザ ID: 123 のプロフィール情報は以下の通りです。 ┃ ┃ ┃ ┃ • 名前: 山田太郎 ┃ ┃ • 経験レベル: 上級 ┃ ┃ ┃ ┃ 経験レベル「上級」 とは、そのユーザが高いスキルや豊富な経験を持っていることを示しています。 ┃ ┃ ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
以上