エージェント・コンテキストは、エージェントの実行前に解決される関数のセット (or 依存関係) を含む辞書で、依存関係をエージェントの説明と指示に注入する方法です。コンテキストを使用してメモリ、動的少数ショットの例、「取得済み」ドキュメント 等を注入することができます。
Agno : ユーザガイド : コンセプト : エージェント – エージェント・コンテキスト
作成 : クラスキャット・セールスインフォメーション
作成日時 : 07/21/2025
バージョン : Agno 1.7.4
* 本記事は docs.agno.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
Agno ユーザガイド : コンセプト : エージェント – エージェント・コンテキスト
エージェント・コンテキストは Agno のもう一つの素晴らしい機能です。コンテキストは、エージェントの実行前に解決される関数のセット (or 依存関係) を含む辞書です。
Info : コンテキストは、依存関係をエージェントの説明と指示に注入する方法です。コンテキストを使用して、メモリ、動的少数ショットの例、「取得済み」ドキュメント 等を注入することができます。
agent_context.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:
"""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)
# Create a Context-Aware Agent that can access real-time HackerNews data
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
# Each function in the context is evaluated when the agent is run,
# think of it as dependency injection for Agents
context={"top_hackernews_stories": get_top_hackernews_stories},
# Alternatively, you can manually add the context to the instructions
instructions=dedent("""\
You are an insightful tech trend observer! 📰
Here are the top stories on HackerNews:
{top_hackernews_stories}\
"""),
# add_state_in_messages will make the `top_hackernews_stories` variable
# available in the instructions
add_state_in_messages=True,
markdown=True,
)
# Example usage
agent.print_response(
"Summarize the top stories on HackerNews and identify any interesting trends.",
stream=True,
)
コンテキスト全体をユーザメッセージに追加する
コンテキスト全体をユーザメッセージに追加するにはadd_context=True を設定します。このようにして、コンテキストを指示に手動で追加する必要がなくなります。
agent_context_instructions.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:
"""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)
# Create a Context-Aware Agent that can access real-time HackerNews data
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
# Each function in the context is resolved when the agent is run,
# think of it as dependency injection for Agents
context={"top_hackernews_stories": get_top_hackernews_stories},
# We can add the entire context dictionary to the instructions
add_context=True,
markdown=True,
)
# Example usage
agent.print_response(
"Summarize the top stories on HackerNews and identify any interesting trends.",
stream=True,
)
以上